extra methods

This commit is contained in:
Łukasz Góreczny 2021-01-22 08:00:03 +01:00
parent b0aaea1474
commit 3f164b7e17
10 changed files with 153 additions and 8 deletions

View File

@ -15,5 +15,6 @@ namespace SessionCompanion.Services.Interfaces
{
Task<Either<SuccessResponse, ErrorResponse>> CreateNewShopKeeper(ShopkeeperWithItemsViewModel shopkeeperWithItemsViewModel);
Task<Either<SuccessResponse, ErrorResponse>> ChangeShopkeeperStatus(int shopkeeperId, bool availability);
Task<Either<ShopkeeperWithItemsDetailsViewModel, ErrorResponse>> GetActiveShkopkeeperWithItems();
}
}

View File

@ -9,11 +9,17 @@ using System.Threading.Tasks;
namespace SessionCompanion.Services.Profiles
{
using SessionCompanion.ViewModels.ShopkeeperItemViewModels;
public class ShopkeeperItemsProfile : Profile
{
public ShopkeeperItemsProfile()
{
CreateMap<ShopkeeperItemViewModel, ShopkeeperItem>().ReverseMap();
CreateMap<ShopkeeperItemDetailsViewModel, ShopkeeperItem>()
.ForMember(vm => vm.Armor, conf => conf.MapFrom(item => item.Armor))
.ForMember(vm => vm.Weapon, conf => conf.MapFrom(item => item.Weapon)).ReverseMap();
}
}
}

View File

@ -16,6 +16,9 @@ namespace SessionCompanion.Services.Profiles
CreateMap<ShopkeeperViewModel, Shopkeeper>().ReverseMap();
CreateMap<Shopkeeper, ShopkeeperWithItemsViewModel>()
.ForMember(vm => vm.Items, conf => conf.MapFrom(items => items.ShopkeeperItems)).ReverseMap();
CreateMap<Shopkeeper, ShopkeeperWithItemsDetailsViewModel>()
.ForMember(vm => vm.Items, conf => conf.MapFrom(items => items.ShopkeeperItems)).ReverseMap();
}
}
}

View File

@ -19,6 +19,7 @@ namespace SessionCompanion.Services.Services
{
public ShopkeeperService(IMapper mapper, IRepository<Shopkeeper> repository) : base(mapper, repository)
{ }
/// <summary>
/// Funkcja zmienia status sprzedawcy
/// </summary>
@ -55,12 +56,13 @@ namespace SessionCompanion.Services.Services
return new ErrorResponse() { StatusCode = 500, Message = e.Message };
}
}
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)
if (activeShopkeeper != null && shopkeeperWithItemsViewModel.IsAvailable)
{
activeShopkeeper.IsAvailable = false;
await Repository.Update(activeShopkeeper);
@ -75,5 +77,23 @@ namespace SessionCompanion.Services.Services
return new ErrorResponse() { StatusCode = 500, Message = e.Message };
}
}
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 };
}
}
}
}

View File

@ -0,0 +1,38 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SessionCompanion.ViewModels.ShopkeeperItemViewModels
{
using SessionCompanion.ViewModels.ArmorViewModels;
using SessionCompanion.ViewModels.WeaponViewModels;
public class ShopkeeperItemDetailsViewModel
{
/// <summary>
/// Id Sprzedawcy
/// </summary>
public int? ShopkeeperId { get; set; }
/// <summary>
/// Id zbroi
/// </summary>
public int? ArmorId { get; set; }
public ArmorViewModel Armor { get; set; }
/// <summary>
/// Id broni
/// </summary>
public int? WeaponId { get; set; }
public WeaponViewModel Weapon { get; set; }
/// <summary>
/// Ilość przedmiotu
/// </summary>
public int Amount { get; set; }
}
}

View File

@ -6,20 +6,26 @@ using System.Threading.Tasks;
namespace SessionCompanion.ViewModels.ShopkeeperItemsViewModels
{
using SessionCompanion.ViewModels.ArmorViewModels;
using SessionCompanion.ViewModels.WeaponViewModels;
public class ShopkeeperItemViewModel
{
/// <summary>
/// Id Sprzedawcy
/// </summary>
public int? ShopkeeperId { get; set; }
/// <summary>
/// Id zbroi
/// </summary>
public int? ArmorId { get; set; }
/// <summary>
/// Id broni
/// </summary>
public int? WeaponId { get; set; }
/// <summary>
/// Ilość przedmiotu
/// </summary>

View File

@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SessionCompanion.ViewModels.ShopkeeperViewModels
{
using SessionCompanion.ViewModels.ShopkeeperItemViewModels;
public class ShopkeeperWithItemsDetailsViewModel
{
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<ShopkeeperItemDetailsViewModel> Items { get; set; }
}
}

View File

@ -46,13 +46,13 @@ export class GameMasterShopkeepersTableComponent implements OnInit {
ActivateShopkeeper(shopkeeperId: number) {
if (this.dataSource.data.find((e) => e.isAvailable) != null) {
this.dataSource.data.find((e) => e.id == shopkeeperId).isAvailable = true;
//TODO podpięcie pod backend zmianę
this.shopkeeperService.ChangeShopkeeperStatus(shopkeeperId, true);
}
}
DeactivateShopkeeper(shopkeeperId: number) {
this.dataSource.data.find((e) => e.id == shopkeeperId).isAvailable = true;
//TODO podpięcie pod backend zmianę
this.dataSource.data.find((e) => e.id == shopkeeperId).isAvailable = false;
this.shopkeeperService.ChangeShopkeeperStatus(shopkeeperId, false);
}
applyFilter(event: Event) {

View File

@ -1,10 +1,11 @@
import { Inject, Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { HttpClient, HttpParams } from '@angular/common/http';
import { Observable, of, throwError } from 'rxjs';
import { ShopkeeperViewModel } from '../types/viewmodels/shopkeeper-viewmodels/ShopkeeperViewModel';
import { Either } from '../types/Either';
import { ErrorResponse } from '../types/ErrorResponse';
import { switchMap } from 'rxjs/operators';
import { SuccessResponse } from '../types/SuccessResponse';
Injectable({
providedIn: 'root',
@ -31,4 +32,11 @@ export class ShopkeeperService {
})
);
}
ChangeShopkeeperStatus(shopkeeperId: number, newStatus: boolean) {
const params = new HttpParams()
.set('shopkeeperId', shopkeeperId.toString())
.set('availability', newStatus.toString());
this.http.post(this.baseUrl + 'getShopkeepers', params);
}
}

View File

@ -11,15 +11,21 @@ using System.Threading.Tasks;
namespace SessionCompanion.Controllers
{
using Microsoft.AspNetCore.SignalR;
using SessionCompanion.Hubs;
[ApiController]
[Route("api/shopkeeper")]
public class ShopkeeperController : Controller
{
private IShopkeeperService _service;
public ShopkeeperController(IShopkeeperService shopkeeperService)
private IHubContext<SessionHub> _sessionHub;
public ShopkeeperController(IShopkeeperService shopkeeperService, IHubContext<SessionHub> sessionHub)
{
_service = shopkeeperService;
this._service = shopkeeperService;
this._sessionHub = sessionHub;
}
/// <summary>
@ -32,6 +38,16 @@ namespace SessionCompanion.Controllers
return _service.Get().ToList();
}
/// <summary>
/// Endpoint zwracający aktywnego sklepikarza
/// </summary>
/// <returns>Lista sklepikarzy</returns>
[HttpGet("getActiveShopkeeper")]
public async Task<Either<ShopkeeperWithItemsDetailsViewModel, ErrorResponse>> GetActiveShopkeepers()
{
return await _service.GetActiveShkopkeeperWithItems();
}
/// <summary>
/// Endpoint służacy do zmiany statusu sklepikarza
/// </summary>
@ -41,7 +57,13 @@ namespace SessionCompanion.Controllers
[HttpPut("changeShopkeeperStatus")]
public async Task<Either<SuccessResponse, ErrorResponse>> ChangeShopkeeperStatus([Required] int shopkeeperId, [Required] bool availability)
{
return await _service.ChangeShopkeeperStatus(shopkeeperId, availability);
var result = await _service.ChangeShopkeeperStatus(shopkeeperId, availability);
if (result.IsLeft && availability)
{
await this._sessionHub.Clients.Group("Players").SendAsync("NewShopkeeperArrived");
}
return result;
}
[HttpPost("createNewShopkeeper")]
@ -51,5 +73,16 @@ namespace SessionCompanion.Controllers
return new ErrorResponse() { StatusCode = 500, Message = "Model is invalid" };
return await _service.CreateNewShopKeeper(shopkeeperWithItemsViewModel);
}
[HttpPost("removeShopkeeper")]
public async Task<Either<SuccessResponse, ErrorResponse>> RemoveShopkeeper([Required] int shopkeeperId)
{
await this._service.Delete(shopkeeperId);
await this._service.SaveAsync();
await this._sessionHub.Clients.Group("Players").SendAsync("ShopkeeperRemoved");
return new SuccessResponse("Delete completed");
}
}
}