SES-154 Endpoint zmiany statusu sklepikarza #80
@ -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>> ChangeShopkeeperStatus(int shopkeeperId, bool availability);
|
||||
}
|
||||
}
|
||||
|
@ -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,41 @@ namespace SessionCompanion.Services.Services
|
||||
{
|
||||
public ShopkeeperService(IMapper mapper, IRepository<Shopkeeper> repository) : base(mapper, repository)
|
||||
{ }
|
||||
/// <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();
|
||||
return new SuccessResponse("Shopkeepers updated") { SuccessCode = 200 };
|
||||
}
|
||||
|
||||
var newActiveShopkeeper = shopkeepers.Where(c => c.Id.Equals(shopkeeperId)).Single();
|
||||
newActiveShopkeeper.IsAvailable = false;
|
||||
await Repository.Update(newActiveShopkeeper);
|
||||
await Repository.Save();
|
||||
return new SuccessResponse("Shopkeepers updated") { SuccessCode = 200 };
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return new ErrorResponse() { StatusCode = 500, Message = e.Message };
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user