diff --git a/SessionCompanion/SessionCompanion.Services/Intefraces/ICharacterArmorService.cs b/SessionCompanion/SessionCompanion.Services/Intefraces/ICharacterArmorService.cs index e380b04..72103af 100644 --- a/SessionCompanion/SessionCompanion.Services/Intefraces/ICharacterArmorService.cs +++ b/SessionCompanion/SessionCompanion.Services/Intefraces/ICharacterArmorService.cs @@ -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 { + Task> ChangeCharacterArmor(int characterId, int newArmorId); Task> GetCharacterArmorsTaskList(int characterId); } } diff --git a/SessionCompanion/SessionCompanion.Services/Intefraces/ICharacterWeaponService.cs b/SessionCompanion/SessionCompanion.Services/Intefraces/ICharacterWeaponService.cs index 8206eb9..ff02169 100644 --- a/SessionCompanion/SessionCompanion.Services/Intefraces/ICharacterWeaponService.cs +++ b/SessionCompanion/SessionCompanion.Services/Intefraces/ICharacterWeaponService.cs @@ -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 { Task> GetCharacterWeaponsList(int characterId); + Task> ChangeCharacterWeapon(CharacterWeaponViewModel model); } } diff --git a/SessionCompanion/SessionCompanion.Services/Services/CharacterArmorService.cs b/SessionCompanion/SessionCompanion.Services/Services/CharacterArmorService.cs index b3d64e0..9413e2f 100644 --- a/SessionCompanion/SessionCompanion.Services/Services/CharacterArmorService.cs +++ b/SessionCompanion/SessionCompanion.Services/Services/CharacterArmorService.cs @@ -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, ICharacterArmorService @@ -23,6 +25,53 @@ namespace SessionCompanion.Services.Services public CharacterArmorService(IMapper mapper, IRepository repository) : base(mapper, repository) { } + public async Task> 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 }; + } + } /// /// Metoda pobiera listę pancerzy konkretnej postaci /// diff --git a/SessionCompanion/SessionCompanion.Services/Services/CharacterWeaponService.cs b/SessionCompanion/SessionCompanion.Services/Services/CharacterWeaponService.cs index 2e54b32..e003791 100644 --- a/SessionCompanion/SessionCompanion.Services/Services/CharacterWeaponService.cs +++ b/SessionCompanion/SessionCompanion.Services/Services/CharacterWeaponService.cs @@ -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, ICharacterWeaponService { @@ -30,5 +32,63 @@ namespace SessionCompanion.Services.Services var result = Mapper.Map>(characterWeapons); return result; } + + public async Task> 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(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 }; + } } } diff --git a/SessionCompanion/SessionCompanion.Services/SessionCompanion.Services.csproj b/SessionCompanion/SessionCompanion.Services/SessionCompanion.Services.csproj index a6d012a..d8f9df8 100644 --- a/SessionCompanion/SessionCompanion.Services/SessionCompanion.Services.csproj +++ b/SessionCompanion/SessionCompanion.Services/SessionCompanion.Services.csproj @@ -10,6 +10,7 @@ + diff --git a/SessionCompanion/SessionCompanion.sln b/SessionCompanion/SessionCompanion.sln index 728a6b1..6153d19 100644 --- a/SessionCompanion/SessionCompanion.sln +++ b/SessionCompanion/SessionCompanion.sln @@ -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 diff --git a/SessionCompanion/SessionCompanion/Controllers/EquipmentController.cs b/SessionCompanion/SessionCompanion/Controllers/EquipmentController.cs index ef1fd92..3314eb5 100644 --- a/SessionCompanion/SessionCompanion/Controllers/EquipmentController.cs +++ b/SessionCompanion/SessionCompanion/Controllers/EquipmentController.cs @@ -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, ErrorResponse>> GetCharacterArmors(int characterId) { - return await this._characterArmorServic.GetCharacterArmorsTaskList(characterId); + return await this._characterArmorService.GetCharacterArmorsTaskList(characterId); } /// @@ -65,5 +65,81 @@ namespace SessionCompanion.Controllers return await this._characterWeaponService.GetCharacterWeaponsList(characterId); } + /// + /// Metoda zmienia uzywaną zbroję danej postaci na taki jaki jest wybrany + /// + /// Id postaci + /// Id nowej zbroi + /// SuccessResponse/ErrorResponse + [HttpPut("changeArmor")] + public async Task> ChangeCharacterArmor(int characterId, int newArmorId) + { + var response = await _characterArmorService.ChangeCharacterArmor(characterId, newArmorId); + return response; + } + + /// + /// Metoda dodaje nową zbroje do danej postaci + /// + /// View model z odpowiednimi parameterami + /// SuccessResponse/ErrorResponse + [HttpPut("addArmor")] + public async Task> 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 }; + } + } + + /// + /// Metoda dodaje broń do danej postaci + /// + /// View model z odpowiednimi parameterami + /// SuccessResponse/ErrorResponse + [HttpPut("addWeapon")] + public async Task> 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 }; + } + } + /// + /// Metoda zmienia broń do danej postaci + /// + /// View model z odpowiednimi parameterami + /// SuccessResponse/ErrorResponse + [HttpPut("changeWeapon")] + public async Task> 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 }; + } + } } }