SES-153 Add new service, to create new shopkeeper with items

This commit is contained in:
Karol Górzyński 2021-01-20 20:54:19 +01:00
parent 69e23bf187
commit 99f177887f
2 changed files with 26 additions and 1 deletions

View File

@ -1,5 +1,7 @@
using SessionCompanion.Database.Tables; using SessionCompanion.Database.Tables;
using SessionCompanion.Extensions.EitherType;
using SessionCompanion.Services.Base; using SessionCompanion.Services.Base;
using SessionCompanion.ViewModels.ApiResponses;
using SessionCompanion.ViewModels.ShopkeeperViewModels; using SessionCompanion.ViewModels.ShopkeeperViewModels;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
@ -11,6 +13,6 @@ namespace SessionCompanion.Services.Interfaces
{ {
public interface IShopkeeperService : IServiceBase<ShopkeeperViewModel, Shopkeeper> public interface IShopkeeperService : IServiceBase<ShopkeeperViewModel, Shopkeeper>
{ {
Task<Either<SuccessResponse, ErrorResponse>> CreateNewShopKeeper(ShopkeeperWithItemsViewModel shopkeeperWithItemsViewModel);
} }
} }

View File

@ -1,8 +1,11 @@
using AutoMapper; using AutoMapper;
using Microsoft.EntityFrameworkCore;
using SessionCompanion.Database.Repositories.Base; using SessionCompanion.Database.Repositories.Base;
using SessionCompanion.Database.Tables; using SessionCompanion.Database.Tables;
using SessionCompanion.Extensions.EitherType;
using SessionCompanion.Services.Base; using SessionCompanion.Services.Base;
using SessionCompanion.Services.Interfaces; using SessionCompanion.Services.Interfaces;
using SessionCompanion.ViewModels.ApiResponses;
using SessionCompanion.ViewModels.ShopkeeperViewModels; using SessionCompanion.ViewModels.ShopkeeperViewModels;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
@ -16,5 +19,25 @@ namespace SessionCompanion.Services.Services
{ {
public ShopkeeperService(IMapper mapper, IRepository<Shopkeeper> repository) : base(mapper, repository) 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 };
}
}
} }
} }