Merge pull request 'SES-153 Endpoint tworzący sklepikarza' (#81) from SES-153 into dev
Reviewed-on: #81
This commit is contained in:
commit
f9c41061e1
@ -1,5 +1,7 @@
|
||||
using SessionCompanion.Database.Tables;
|
||||
using SessionCompanion.Extensions.EitherType;
|
||||
using SessionCompanion.Services.Base;
|
||||
using SessionCompanion.ViewModels.ApiResponses;
|
||||
using SessionCompanion.ViewModels.ShopkeeperViewModels;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
@ -11,6 +13,6 @@ namespace SessionCompanion.Services.Interfaces
|
||||
{
|
||||
public interface IShopkeeperService : IServiceBase<ShopkeeperViewModel, Shopkeeper>
|
||||
{
|
||||
|
||||
Task<Either<SuccessResponse, ErrorResponse>> CreateNewShopKeeper(ShopkeeperWithItemsViewModel shopkeeperWithItemsViewModel);
|
||||
}
|
||||
}
|
||||
|
@ -14,6 +14,8 @@ namespace SessionCompanion.Services.Profiles
|
||||
public ShopkeeperProfile()
|
||||
{
|
||||
CreateMap<ShopkeeperViewModel, Shopkeeper>().ReverseMap();
|
||||
CreateMap<Shopkeeper, ShopkeeperWithItemsViewModel>()
|
||||
.ForMember(vm => vm.Items, conf => conf.MapFrom(items => items.ShopkeeperItems)).ReverseMap();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,8 +1,11 @@
|
||||
using AutoMapper;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using SessionCompanion.Database.Repositories.Base;
|
||||
using SessionCompanion.Database.Tables;
|
||||
using SessionCompanion.Extensions.EitherType;
|
||||
using SessionCompanion.Services.Base;
|
||||
using SessionCompanion.Services.Interfaces;
|
||||
using SessionCompanion.ViewModels.ApiResponses;
|
||||
using SessionCompanion.ViewModels.ShopkeeperViewModels;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
@ -16,5 +19,25 @@ namespace SessionCompanion.Services.Services
|
||||
{
|
||||
public ShopkeeperService(IMapper mapper, IRepository<Shopkeeper> repository) : base(mapper, repository)
|
||||
{ }
|
||||
public async Task<Either<SuccessResponse, ErrorResponse>> CreateNewShopKeeper(ShopkeeperWithItemsViewModel shopkeeperWithItemsViewModel)
|
||||
{
|
||||
try
|
||||
{
|
||||
var activeShopkeeper = await Repository.Get(c => c.IsAvailable.Equals(true)).SingleAsync();
|
||||
if (activeShopkeeper is not null && shopkeeperWithItemsViewModel.IsAvailable)
|
||||
{
|
||||
activeShopkeeper.IsAvailable = false;
|
||||
await Repository.Update(activeShopkeeper);
|
||||
}
|
||||
var result = Mapper.Map<Shopkeeper>(shopkeeperWithItemsViewModel);
|
||||
await Repository.Create(result);
|
||||
await Repository.Save();
|
||||
return new SuccessResponse("New shopkeeper created");
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return new ErrorResponse() { StatusCode = 500, Message = e.Message };
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -11,7 +11,7 @@ namespace SessionCompanion.ViewModels.ShopkeeperItemsViewModels
|
||||
/// <summary>
|
||||
/// Id Sprzedawcy
|
||||
/// </summary>
|
||||
public int ShopkeeperId { get; set; }
|
||||
public int? ShopkeeperId { get; set; }
|
||||
/// <summary>
|
||||
/// Id zbroi
|
||||
/// </summary>
|
||||
@ -20,5 +20,9 @@ namespace SessionCompanion.ViewModels.ShopkeeperItemsViewModels
|
||||
/// Id broni
|
||||
/// </summary>
|
||||
public int? WeaponId { get; set; }
|
||||
/// <summary>
|
||||
/// Ilość przedmiotu
|
||||
/// </summary>
|
||||
public int Amount { get; set; }
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,27 @@
|
||||
using SessionCompanion.ViewModels.ShopkeeperItemsViewModels;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SessionCompanion.ViewModels.ShopkeeperViewModels
|
||||
{
|
||||
public class ShopkeeperWithItemsViewModel
|
||||
{
|
||||
public int? Id { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Nazwa sklepikarza
|
||||
/// </summary>
|
||||
public string Name { get; set; }
|
||||
/// <summary>
|
||||
/// Status sklepikarza
|
||||
/// </summary>
|
||||
public bool IsAvailable { get; set; }
|
||||
/// <summary>
|
||||
/// Lista przedmiotów danego sklepikarza
|
||||
/// </summary>
|
||||
public IEnumerable<ShopkeeperItemViewModel> Items { get; set; }
|
||||
}
|
||||
}
|
@ -5,6 +5,7 @@ using SessionCompanion.ViewModels.ApiResponses;
|
||||
using SessionCompanion.ViewModels.ShopkeeperViewModels;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
@ -29,5 +30,13 @@ namespace SessionCompanion.Controllers
|
||||
{
|
||||
return _service.Get().ToList();
|
||||
}
|
||||
|
||||
[HttpPost("createNewShopkeeper")]
|
||||
public async Task<Either<SuccessResponse, ErrorResponse>> CreateNewShopKeeper([Required] ShopkeeperWithItemsViewModel shopkeeperWithItemsViewModel)
|
||||
{
|
||||
if (!ModelState.IsValid)
|
||||
return new ErrorResponse() { StatusCode = 500, Message = "Model is invalid" };
|
||||
return await _service.CreateNewShopKeeper(shopkeeperWithItemsViewModel);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user