From b138651a8f475eeb41ca9ffdcca141412158a957 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20G=C3=B3reczny?= Date: Fri, 18 Dec 2020 11:18:48 +0100 Subject: [PATCH] SES-107 Added new method to return characters list --- .../Intefraces/ICharacterService.cs | 1 + .../Services/CharacterService.cs | 16 ++++++++++++ .../Controllers/CharacterController.cs | 26 +++++++++++++++++++ 3 files changed, 43 insertions(+) diff --git a/SessionCompanion/SessionCompanion.Services/Intefraces/ICharacterService.cs b/SessionCompanion/SessionCompanion.Services/Intefraces/ICharacterService.cs index 06bdf47..876ce7f 100644 --- a/SessionCompanion/SessionCompanion.Services/Intefraces/ICharacterService.cs +++ b/SessionCompanion/SessionCompanion.Services/Intefraces/ICharacterService.cs @@ -10,5 +10,6 @@ namespace SessionCompanion.Services.Interfaces { public interface ICharacterService : IServiceBase { + Task> GetUserLoginCharacters(int userId); } } diff --git a/SessionCompanion/SessionCompanion.Services/Services/CharacterService.cs b/SessionCompanion/SessionCompanion.Services/Services/CharacterService.cs index 939ea90..28c96e8 100644 --- a/SessionCompanion/SessionCompanion.Services/Services/CharacterService.cs +++ b/SessionCompanion/SessionCompanion.Services/Services/CharacterService.cs @@ -21,5 +21,21 @@ namespace SessionCompanion.Services.Services { public CharacterService(IMapper mapper, IRepository repository) : base(mapper, repository) { } + + /// + /// Funkcja zwraca listę postaci przypisanych do podanego użytkownika + /// + /// identyfikator użytkownika + /// Lista postaci dosępnych dla podanego użytkownika + public async Task> GetUserLoginCharacters(int userId) + { + var characters = await Repository.Get(c => c.UserId.Equals(userId)) + .Include(x => x.Biography) + .Include(x => x.Statistics) + .Include(x => x.Biography).ThenInclude(b => b.Class).ToListAsync(); + var result = Mapper.Map>(characters); + return result; + } + } } diff --git a/SessionCompanion/SessionCompanion/Controllers/CharacterController.cs b/SessionCompanion/SessionCompanion/Controllers/CharacterController.cs index 52507ad..cb6e42e 100644 --- a/SessionCompanion/SessionCompanion/Controllers/CharacterController.cs +++ b/SessionCompanion/SessionCompanion/Controllers/CharacterController.cs @@ -7,6 +7,10 @@ using SessionCompanion.ViewModels.CharacterViewModels; namespace SessionCompanion.Controllers { + using System.Collections.Generic; + using System.ComponentModel.DataAnnotations; + using System.Linq; + [Route("api/character")] [ApiController] public class CharacterController : Controller @@ -35,5 +39,27 @@ namespace SessionCompanion.Controllers }; return characterViewModel; } + + /// + /// Metoda zwraca listę postaci przypisanych do danego użytkownika + /// + /// Identyfikator użytkownika + /// Lista postać lub wiadomość błędu + [HttpGet("userCharactersList")] + public async Task, ErrorResponse>> GetCharacterListForUser([Required] int userId) + { + var charactersList = await this._service.GetUserLoginCharacters(userId); + + if (charactersList.Any()) + { + return charactersList.ToList(); + } + + return new ErrorResponse() + { + StatusCode = 204, + Message = "No characters with given user id" + }; + } } } \ No newline at end of file