SES-148 Endpointy dla Armor i Weapon #71
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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,61 @@ 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 weaponsInUse = await Repository.Get(c => c.CharacterId.Equals(model.CharacterId)).Where(w => w.InUse.Equals(true)).ToListAsync();
|
||||
|
||||
|
||||
s426135 marked this conversation as resolved
s426134
commented
to chyba juz można wywalić :P to chyba juz można wywalić :P
|
||||
var weapon = Mapper.Map<CharacterWeapon>(model);
|
||||
|
||||
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 };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -86,6 +86,8 @@ namespace SessionCompanion.Controllers
|
||||
[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);
|
||||
@ -97,6 +99,7 @@ namespace SessionCompanion.Controllers
|
||||
return new ErrorResponse() { StatusCode = 500, Message = e.Message };
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Metoda dodaje Weapon do danej postaci
|
||||
/// </summary>
|
||||
@ -118,5 +121,21 @@ namespace SessionCompanion.Controllers
|
||||
return new ErrorResponse() { StatusCode = 500, Message = e.Message };
|
||||
}
|
||||
}
|
||||
|
||||
[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 };
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user
można to wszystko skrócić dodając lub, czyli c.CharacterId.Equals(model.CharacterId) && ( jestWLewej || jestWPrawej || jest równa weapon id z modelu )
Neh, tego potrzeba tak jak jest, musze miec wszystkie bronie jakie są, aby mieć dostęp do Id