Merge pull request 'SES-148 Endpointy dla Armor i Weapon' (#71) from SES-148 into dev

Reviewed-on: #71
This commit is contained in:
Łukasz Góreczny 2021-01-14 16:45:04 +01:00
commit 2005670077
7 changed files with 199 additions and 4 deletions

View File

@ -7,11 +7,14 @@ namespace SessionCompanion.Services.Interfaces
using System.Threading.Tasks;
using SessionCompanion.Database.Tables;
using SessionCompanion.Extensions.EitherType;
using SessionCompanion.Services.Base;
using SessionCompanion.ViewModels.ApiResponses;
using SessionCompanion.ViewModels.CharacterArmorViewModels;
public interface ICharacterArmorService : IServiceBase<CharacterArmorViewModel, CharacterArmor>
{
Task<Either<SuccessResponse, ErrorResponse>> ChangeCharacterArmor(int characterId, int newArmorId);
Task<List<CharacterArmorViewModelDetails>> GetCharacterArmorsTaskList(int characterId);
}
}

View File

@ -4,11 +4,14 @@ using SessionCompanion.ViewModels.CharacterWeaponViewModels;
namespace SessionCompanion.Services.Interfaces
{
using SessionCompanion.Extensions.EitherType;
using SessionCompanion.ViewModels.ApiResponses;
using System.Collections.Generic;
using System.Threading.Tasks;
public interface ICharacterWeaponService : IServiceBase<CharacterWeaponViewModel, CharacterWeapon>
{
Task<List<CharacterWeaponWithWeaponDetailsViewModel>> GetCharacterWeaponsList(int characterId);
Task<Either<SuccessResponse, ErrorResponse>> ChangeCharacterWeapon(CharacterWeaponViewModel model);
}
}

View File

@ -15,7 +15,9 @@ namespace SessionCompanion.Services.Services
using SessionCompanion.Database.Tables;
using SessionCompanion.Services.Base;
using SessionCompanion.Services.Interfaces;
using SessionCompanion.ViewModels.ApiResponses;
using SessionCompanion.ViewModels.CharacterArmorViewModels;
using SessionCompanion.Extensions.EitherType;
using SessionCompanion.ViewModels.CharacterOtherEquipmentViewModels;
public class CharacterArmorService : ServiceBase<CharacterArmorViewModel, CharacterArmor>, ICharacterArmorService
@ -23,6 +25,53 @@ namespace SessionCompanion.Services.Services
public CharacterArmorService(IMapper mapper, IRepository<CharacterArmor> repository) : base(mapper, repository)
{ }
public async Task<Either<SuccessResponse, ErrorResponse>> ChangeCharacterArmor(int characterId, int newArmorId)
{
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)
return new ErrorResponse() { StatusCode = 204, Message = "No armor to change to" };
if (armorInUse is null)
{
armorToUse.InUse = true;
try
{
await Repository.Update(armorToUse);
await Repository.Save();
return new SuccessResponse("Character armor updated") { SuccessCode = 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();
return new SuccessResponse("Character armor updated") { SuccessCode = 204 };
}
catch (Exception e)
{
return new ErrorResponse() { StatusCode = 500, Message = e.Message };
}
}
/// <summary>
/// Metoda pobiera listę pancerzy konkretnej postaci
/// </summary>

View File

@ -12,6 +12,8 @@ namespace SessionCompanion.Services.Services
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using SessionCompanion.Extensions.EitherType;
using SessionCompanion.ViewModels.ApiResponses;
public class CharacterWeaponService : ServiceBase<CharacterWeaponViewModel, CharacterWeapon>, ICharacterWeaponService
{
@ -30,5 +32,63 @@ namespace SessionCompanion.Services.Services
var result = Mapper.Map<List<CharacterWeaponWithWeaponDetailsViewModel>>(characterWeapons);
return result;
}
public async Task<Either<SuccessResponse,ErrorResponse>> ChangeCharacterWeapon(CharacterWeaponViewModel model)
{
// Dodaj optional rozbro postac
var allWeapons = await Repository.Get(c => c.CharacterId.Equals(model.CharacterId)).AsNoTracking().ToListAsync();
var weaponsInUse = allWeapons.Where(w => w.InUse.Equals(true)).ToList();
var weapon = Mapper.Map<CharacterWeapon>(model);
weapon.Id = allWeapons.Where(w => w.WeaponId.Equals(model.WeaponId)).Select(x => x.Id).FirstOrDefault();
if (weaponsInUse.Count() == 0)
{
// no weapon in use
// just use new one
await Repository.Update(weapon);
await Repository.Save();
return new SuccessResponse("Weapon changed") { SuccessCode = 200 };
}
var weaponInBothHands = weaponsInUse.Where(w => w.HoldInLeftHand.Equals(true) && w.HoldInRightHand.Equals(true));
if ((model.HoldInLeftHand && model.HoldInRightHand) || (weaponInBothHands.Count() > 0))
{
// our model weapon uses both hands
// or there is weapon already in both hands
foreach (var w in weaponsInUse)
{
w.InUse = false;
w.HoldInLeftHand = false;
w.HoldInRightHand = false;
await Repository.Update(w);
}
await Repository.Update(weapon);
await Repository.Save();
return new SuccessResponse("Weapon changed") { SuccessCode = 200 };
}
var weaponsToChange = weaponsInUse.Where(w => w.HoldInLeftHand.Equals(model.HoldInLeftHand) && w.HoldInRightHand.Equals(model.HoldInRightHand));
if (weaponsToChange.Count() == 1)
{
// there is weapon in the same hand set as our
// we update it
var weaponToChange = weaponsToChange.Single();
weaponToChange.InUse = false;
weaponToChange.HoldInLeftHand = false;
weaponToChange.HoldInRightHand = false;
await Repository.Update(weaponToChange);
await Repository.Update(weapon);
await Repository.Save();
return new SuccessResponse("Weapon changed") { SuccessCode = 200 };
}
// weapon is armed in empty hand
await Repository.Update(weapon);
await Repository.Save();
return new SuccessResponse("Weapon changed") { SuccessCode = 200 };
}
}
}

View File

@ -10,6 +10,7 @@
<ItemGroup>
<ProjectReference Include="..\SessionCompanion.Database\SessionCompanion.Database.csproj" />
<ProjectReference Include="..\SessionCompanion.Extensions\SessionCompanion.Extensions.csproj" />
<ProjectReference Include="..\SessionCompanion.ViewModels\SessionCompanion.ViewModels.csproj" />
</ItemGroup>

View File

@ -10,10 +10,13 @@ EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SessionCompanion.ViewModels", "SessionCompanion.ViewModels\SessionCompanion.ViewModels.csproj", "{7762AA75-7B60-4F28-B80A-B03E39140F89}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SessionCompanion.Services", "SessionCompanion.Services\SessionCompanion.Services.csproj", "{C0A172ED-0F4C-4E78-8B64-28E2A756F62F}"
ProjectSection(ProjectDependencies) = postProject
{1EE35EB3-C703-407C-B390-5605A0A46884} = {1EE35EB3-C703-407C-B390-5605A0A46884}
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SessionCompanion.Extensions", "SessionCompanion.Extensions\SessionCompanion.Extensions.csproj", "{1EE35EB3-C703-407C-B390-5605A0A46884}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SessionCompanion.XUnitTests", "SessionCompanion.XUnitTests\SessionCompanion.XUnitTests.csproj", "{B8A4DAF6-DD33-4B35-99B8-A1D060EE1869}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SessionCompanion.XUnitTests", "SessionCompanion.XUnitTests\SessionCompanion.XUnitTests.csproj", "{B8A4DAF6-DD33-4B35-99B8-A1D060EE1869}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution

View File

@ -17,7 +17,7 @@ namespace SessionCompanion.Controllers
[ApiController]
public class EquipmentController : Controller
{
private readonly ICharacterArmorService _characterArmorServic;
private readonly ICharacterArmorService _characterArmorService;
private readonly ICharacterOtherEquipmentService _characterOtherEquipmentService;
@ -27,7 +27,7 @@ namespace SessionCompanion.Controllers
ICharacterOtherEquipmentService characterOtherEquipmentService,
ICharacterWeaponService characterWeaponService)
{
this._characterArmorServic = characterArmorService;
this._characterArmorService = characterArmorService;
this._characterOtherEquipmentService = characterOtherEquipmentService;
this._characterWeaponService = characterWeaponService;
}
@ -51,7 +51,7 @@ namespace SessionCompanion.Controllers
[HttpGet("getArmors")]
public async Task<Either<List<CharacterArmorViewModelDetails>, ErrorResponse>> GetCharacterArmors(int characterId)
{
return await this._characterArmorServic.GetCharacterArmorsTaskList(characterId);
return await this._characterArmorService.GetCharacterArmorsTaskList(characterId);
}
/// <summary>
@ -65,5 +65,81 @@ namespace SessionCompanion.Controllers
return await this._characterWeaponService.GetCharacterWeaponsList(characterId);
}
/// <summary>
/// Metoda zmienia uzywaną zbroję danej postaci na taki jaki jest wybrany
/// </summary>
/// <param name="characterId"> Id postaci </param>
/// <param name="newArmorId"> Id nowej zbroi </param>
/// <returns>SuccessResponse/ErrorResponse</returns>
[HttpPut("changeArmor")]
public async Task<Either<SuccessResponse, ErrorResponse>> ChangeCharacterArmor(int characterId, int newArmorId)
{
var response = await _characterArmorService.ChangeCharacterArmor(characterId, newArmorId);
return response;
}
/// <summary>
/// Metoda dodaje nową zbroje do danej postaci
/// </summary>
/// <param name="characterArmorViewModel"> View model z odpowiednimi parameterami </param>
/// <returns>SuccessResponse/ErrorResponse</returns>
[HttpPut("addArmor")]
public async Task<Either<SuccessResponse, ErrorResponse>> AddCharacterArmor(CharacterArmorViewModel characterArmorViewModel)
{
if (!ModelState.IsValid)
return new ErrorResponse() { StatusCode = 500, Message = "Invalid model!" };
try
{
await _characterArmorService.Create(characterArmorViewModel);
await _characterArmorService.SaveAsync();
return new SuccessResponse("Armor added to character") { SuccessCode = 200 };
}
catch (Exception e)
{
return new ErrorResponse() { StatusCode = 500, Message = e.Message };
}
}
/// <summary>
/// Metoda dodaje broń do danej postaci
/// </summary>
/// <param name="characterWeaponViewModel"> View model z odpowiednimi parameterami </param>
/// <returns>SuccessResponse/ErrorResponse</returns>
[HttpPut("addWeapon")]
public async Task<Either<SuccessResponse, ErrorResponse>> AddCharacterWeapon(CharacterWeaponViewModel characterWeaponViewModel)
{
if (!ModelState.IsValid)
return new ErrorResponse() { StatusCode = 500, Message = "Invalid model!" };
try
{
await _characterWeaponService.Create(characterWeaponViewModel);
await _characterWeaponService.SaveAsync();
return new SuccessResponse("Weapon added to character") { SuccessCode = 200 };
}
catch (Exception e)
{
return new ErrorResponse() { StatusCode = 500, Message = e.Message };
}
}
/// <summary>
/// Metoda zmienia broń do danej postaci
/// </summary>
/// <param name=characterWeaponViewModel> View model z odpowiednimi parameterami </param>
/// <returns>SuccessResponse/ErrorResponse</returns>
[HttpPut("changeWeapon")]
public async Task<Either<SuccessResponse, ErrorResponse>> ChangeCharacterWeapon(CharacterWeaponViewModel characterWeaponViewModel)
{
if (!ModelState.IsValid)
return new ErrorResponse() { StatusCode = 500, Message = "Invalid model!" };
try
{
var response = await _characterWeaponService.ChangeCharacterWeapon(characterWeaponViewModel);
return response;
}
catch (Exception e)
{
return new ErrorResponse() { StatusCode = 500, Message = e.Message };
}
}
}
}