SES-158 Added function to send message to player #83

Merged
s426128 merged 4 commits from SES-158 into dev 2021-01-21 17:51:37 +01:00
6 changed files with 69 additions and 2 deletions
Showing only changes of commit 8a7426cda5 - 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

@ -14,6 +14,8 @@ namespace SessionCompanion.Services.Profiles
public ShopkeeperProfile()
{
CreateMap<ShopkeeperViewModel, Shopkeeper>().ReverseMap();
CreateMap<Shopkeeper, ShopkeeperWithItemsViewModel>()
.ForMember(vm => vm.Items, conf => conf.MapFrom(items => items.ShopkeeperItems)).ReverseMap();
}
}
}

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

View File

@ -11,7 +11,7 @@ namespace SessionCompanion.ViewModels.ShopkeeperItemsViewModels
/// <summary>
/// Id Sprzedawcy
/// </summary>
public int ShopkeeperId { get; set; }
public int? ShopkeeperId { get; set; }
/// <summary>
/// Id zbroi
/// </summary>
@ -20,5 +20,9 @@ namespace SessionCompanion.ViewModels.ShopkeeperItemsViewModels
/// Id broni
/// </summary>
public int? WeaponId { get; set; }
/// <summary>
/// Ilość przedmiotu
/// </summary>
public int Amount { get; set; }
}
}

View File

@ -0,0 +1,27 @@
using SessionCompanion.ViewModels.ShopkeeperItemsViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SessionCompanion.ViewModels.ShopkeeperViewModels
{
public class ShopkeeperWithItemsViewModel
{
public int? Id { get; set; }
/// <summary>
/// Nazwa sklepikarza
/// </summary>
public string Name { get; set; }
/// <summary>
/// Status sklepikarza
/// </summary>
public bool IsAvailable { get; set; }
/// <summary>
/// Lista przedmiotów danego sklepikarza
/// </summary>
public IEnumerable<ShopkeeperItemViewModel> Items { get; set; }
}
}

View File

@ -5,6 +5,7 @@ using SessionCompanion.ViewModels.ApiResponses;
using SessionCompanion.ViewModels.ShopkeeperViewModels;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
@ -29,5 +30,13 @@ namespace SessionCompanion.Controllers
{
return _service.Get().ToList();
}
[HttpPost("createNewShopkeeper")]
public async Task<Either<SuccessResponse, ErrorResponse>> CreateNewShopKeeper([Required] ShopkeeperWithItemsViewModel shopkeeperWithItemsViewModel)
{
if (!ModelState.IsValid)
return new ErrorResponse() { StatusCode = 500, Message = "Model is invalid" };
return await _service.CreateNewShopKeeper(shopkeeperWithItemsViewModel);
}
}
}