2021-01-20 18:54:56 +01:00
|
|
|
|
using AutoMapper;
|
2021-01-20 19:56:51 +01:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2021-01-20 18:54:56 +01:00
|
|
|
|
using SessionCompanion.Database.Repositories.Base;
|
|
|
|
|
using SessionCompanion.Database.Tables;
|
2021-01-20 19:56:51 +01:00
|
|
|
|
using SessionCompanion.Extensions.EitherType;
|
2021-01-20 18:54:56 +01:00
|
|
|
|
using SessionCompanion.Services.Base;
|
|
|
|
|
using SessionCompanion.Services.Interfaces;
|
2021-01-20 19:56:51 +01:00
|
|
|
|
using SessionCompanion.ViewModels.ApiResponses;
|
2021-01-20 18:54:56 +01:00
|
|
|
|
using SessionCompanion.ViewModels.ShopkeeperViewModels;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace SessionCompanion.Services.Services
|
|
|
|
|
{
|
|
|
|
|
public class ShopkeeperService : ServiceBase<ShopkeeperViewModel, Shopkeeper>, IShopkeeperService
|
|
|
|
|
{
|
|
|
|
|
public ShopkeeperService(IMapper mapper, IRepository<Shopkeeper> repository) : base(mapper, repository)
|
|
|
|
|
{ }
|
2021-01-22 08:00:03 +01:00
|
|
|
|
|
2021-01-20 19:56:51 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Funkcja zmienia status sprzedawcy
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="shopkeeperId"></param>
|
|
|
|
|
/// <param name="availability"></param>
|
|
|
|
|
/// <returns>SuccesResponse/ErrorResponse</returns>
|
|
|
|
|
public async Task<Either<SuccessResponse, ErrorResponse>> ChangeShopkeeperStatus(int shopkeeperId, bool availability)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var shopkeepers = await Repository.Get().ToListAsync();
|
|
|
|
|
if (availability)
|
|
|
|
|
{
|
|
|
|
|
foreach (var shopkeeper in shopkeepers)
|
|
|
|
|
{
|
|
|
|
|
if (shopkeeper.Id.Equals(shopkeeperId))
|
|
|
|
|
shopkeeper.IsAvailable = true;
|
|
|
|
|
else
|
|
|
|
|
shopkeeper.IsAvailable = false;
|
|
|
|
|
await Repository.Update(shopkeeper);
|
|
|
|
|
}
|
|
|
|
|
await Repository.Save();
|
2021-01-22 08:42:52 +01:00
|
|
|
|
return new SuccessResponse("Shopkeepers updated") { StatusCode = 200 };
|
2021-01-20 19:56:51 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var newActiveShopkeeper = shopkeepers.Where(c => c.Id.Equals(shopkeeperId)).Single();
|
|
|
|
|
newActiveShopkeeper.IsAvailable = false;
|
|
|
|
|
await Repository.Update(newActiveShopkeeper);
|
|
|
|
|
await Repository.Save();
|
2021-01-22 08:42:52 +01:00
|
|
|
|
return new SuccessResponse("Shopkeepers updated") { StatusCode = 200 };
|
2021-01-20 19:56:51 +01:00
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
return new ErrorResponse() { StatusCode = 500, Message = e.Message };
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-01-22 08:00:03 +01:00
|
|
|
|
|
2021-01-20 20:54:19 +01:00
|
|
|
|
public async Task<Either<SuccessResponse, ErrorResponse>> CreateNewShopKeeper(ShopkeeperWithItemsViewModel shopkeeperWithItemsViewModel)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var activeShopkeeper = await Repository.Get(c => c.IsAvailable.Equals(true)).SingleAsync();
|
2021-01-22 08:00:03 +01:00
|
|
|
|
if (activeShopkeeper != null && shopkeeperWithItemsViewModel.IsAvailable)
|
2021-01-20 20:54:19 +01:00
|
|
|
|
{
|
|
|
|
|
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 };
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-01-22 08:00:03 +01:00
|
|
|
|
|
|
|
|
|
public async Task<Either<ShopkeeperWithItemsDetailsViewModel, ErrorResponse>> GetActiveShkopkeeperWithItems()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var activeShopkeeper = await Repository.Get(c => c.IsAvailable.Equals(true))
|
|
|
|
|
.Include(t => t.ShopkeeperItems)
|
|
|
|
|
.ThenInclude(t => t.Armor)
|
|
|
|
|
.Include(t => t.ShopkeeperItems)
|
|
|
|
|
.ThenInclude(t => t.Weapon).SingleAsync();
|
|
|
|
|
var result = Mapper.Map<ShopkeeperWithItemsDetailsViewModel>(activeShopkeeper);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
return new ErrorResponse() { StatusCode = 500, Message = e.Message };
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-01-20 18:54:56 +01:00
|
|
|
|
}
|
|
|
|
|
}
|