SES-153 Endpoint tworzący sklepikarza #81

Merged
s426134 merged 4 commits from SES-153 into dev 2021-01-21 17:36:34 +01:00
2 changed files with 26 additions and 1 deletions
Showing only changes of commit 99f177887f - Show all commits

View File

@ -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);
}
}

View File

@ -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 };
}
}
}
}