session-companion/SessionCompanion/SessionCompanion.Services/Services/CharacterArmorService.cs

89 lines
3.4 KiB
C#
Raw Normal View History

2020-12-27 19:47:13 +01:00
using System;
using System.Collections.Generic;
using System.Text;
namespace SessionCompanion.Services.Services
{
using System.Linq;
using System.Threading.Tasks;
using AutoMapper;
using Microsoft.EntityFrameworkCore;
using SessionCompanion.Database.Repositories.Base;
using SessionCompanion.Database.Tables;
using SessionCompanion.Services.Base;
using SessionCompanion.Services.Interfaces;
using SessionCompanion.ViewModels.ApiResponses;
2020-12-27 19:47:13 +01:00
using SessionCompanion.ViewModels.CharacterArmorViewModels;
2021-01-13 09:37:20 +01:00
using SessionCompanion.Extensions.EitherType;
using SessionCompanion.ViewModels.CharacterOtherEquipmentViewModels;
2020-12-27 19:47:13 +01:00
public class CharacterArmorService : ServiceBase<CharacterArmorViewModel, CharacterArmor>, ICharacterArmorService
{
public CharacterArmorService(IMapper mapper, IRepository<CharacterArmor> repository) : base(mapper, repository)
{ }
public async Task<Either<SuccessResponse, ErrorResponse>> ChangeCharacterArmor(int characterId, int newArmorId)
{
2021-01-13 17:10:41 +01:00
CharacterArmor armorInUse = new CharacterArmor();
CharacterArmor armorToUse = new CharacterArmor();
try
{
armorInUse = await Repository.Get(c => c.CharacterId.Equals(characterId))
.Include(a => a.Armor).Where(x => x.InUse == true).SingleAsync();
armorToUse = await Repository.Get(c => c.ArmorId.Equals(newArmorId) && c.CharacterId.Equals(characterId)).SingleAsync();
}
catch (Exception e)
{
return new ErrorResponse() { StatusCode = 500, Message = e.Message };
}
if (armorToUse is null)
2021-01-13 21:50:21 +01:00
return new ErrorResponse() { StatusCode = 204, Message = "No armor to change to" };
2021-01-13 17:27:57 +01:00
if (armorInUse is null)
{
armorToUse.InUse = true;
try
{
await Repository.Update(armorToUse);
await Repository.Save();
2021-01-20 21:08:18 +01:00
return new SuccessResponse("Character armor updated") { StatusCode = 200 };
}
catch (Exception e)
{
return new ErrorResponse() { StatusCode = 500, Message = e.Message };
}
}
armorInUse.InUse = false;
armorToUse.InUse = true;
try
{
await Repository.Update(armorInUse);
await Repository.Update(armorToUse);
await Repository.Save();
2021-01-20 21:08:18 +01:00
return new SuccessResponse("Character armor updated") { StatusCode = 204 };
}
catch (Exception e)
{
return new ErrorResponse() { StatusCode = 500, Message = e.Message };
}
}
/// <summary>
/// Metoda pobiera listę pancerzy konkretnej postaci
/// </summary>
/// <param name="characterId"> Id postaci </param>
/// <returns> Lista pancerzy posiadanych przez postać </returns>
public async Task<List<CharacterArmorViewModelDetails>> GetCharacterArmorsTaskList(int characterId)
{
var characterWeapons = await Repository.Get().Where(a => a.CharacterId.Equals(characterId)).Include(a => a.Armor).ToListAsync();
var result = Mapper.Map<List<CharacterArmorViewModelDetails>>(characterWeapons);
return result;
}
2020-12-27 19:47:13 +01:00
}
}