SES-153 Endpoint tworzący sklepikarza #81
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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 };
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user