SES-107 Added new method to return characters list

This commit is contained in:
Łukasz Góreczny 2020-12-18 11:18:48 +01:00
parent c384b279cb
commit b138651a8f
3 changed files with 43 additions and 0 deletions

View File

@ -10,5 +10,6 @@ namespace SessionCompanion.Services.Interfaces
{ {
public interface ICharacterService : IServiceBase<CharacterViewModel, Character> public interface ICharacterService : IServiceBase<CharacterViewModel, Character>
{ {
Task<IEnumerable<CharacterForLoginViewModel>> GetUserLoginCharacters(int userId);
} }
} }

View File

@ -21,5 +21,21 @@ namespace SessionCompanion.Services.Services
{ {
public CharacterService(IMapper mapper, IRepository<Character> repository) : base(mapper, repository) public CharacterService(IMapper mapper, IRepository<Character> repository) : base(mapper, repository)
{ } { }
/// <summary>
/// Funkcja zwraca listę postaci przypisanych do podanego użytkownika
/// </summary>
/// <param name="userId">identyfikator użytkownika</param>
/// <returns>Lista postaci dosępnych dla podanego użytkownika</returns>
public async Task<IEnumerable<CharacterForLoginViewModel>> 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<IEnumerable<CharacterForLoginViewModel>>(characters);
return result;
}
} }
} }

View File

@ -7,6 +7,10 @@ using SessionCompanion.ViewModels.CharacterViewModels;
namespace SessionCompanion.Controllers namespace SessionCompanion.Controllers
{ {
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
[Route("api/character")] [Route("api/character")]
[ApiController] [ApiController]
public class CharacterController : Controller public class CharacterController : Controller
@ -35,5 +39,27 @@ namespace SessionCompanion.Controllers
}; };
return characterViewModel; return characterViewModel;
} }
/// <summary>
/// Metoda zwraca listę postaci przypisanych do danego użytkownika
/// </summary>
/// <param name="userId"> Identyfikator użytkownika </param>
/// <returns> Lista postać lub wiadomość błędu </returns>
[HttpGet("userCharactersList")]
public async Task<Either<List<CharacterForLoginViewModel>, 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"
};
}
} }
} }