From 93a6631cc303424557dd80ea6aaef33966d6a683 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karol=20G=C3=B3rzy=C5=84ski?= Date: Wed, 20 Jan 2021 20:53:34 +0100 Subject: [PATCH 1/4] SES-153 Change and add new ViewModels --- .../ShopkeeperItemViewModel.cs | 6 ++++- .../ShopkeeperWithItemsViewModel.cs | 27 +++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 SessionCompanion/SessionCompanion.ViewModels/ShopkeeperViewModels/ShopkeeperWithItemsViewModel.cs diff --git a/SessionCompanion/SessionCompanion.ViewModels/ShopkeeperItemViewModels/ShopkeeperItemViewModel.cs b/SessionCompanion/SessionCompanion.ViewModels/ShopkeeperItemViewModels/ShopkeeperItemViewModel.cs index 9854685..ca6e236 100644 --- a/SessionCompanion/SessionCompanion.ViewModels/ShopkeeperItemViewModels/ShopkeeperItemViewModel.cs +++ b/SessionCompanion/SessionCompanion.ViewModels/ShopkeeperItemViewModels/ShopkeeperItemViewModel.cs @@ -11,7 +11,7 @@ namespace SessionCompanion.ViewModels.ShopkeeperItemsViewModels /// /// Id Sprzedawcy /// - public int ShopkeeperId { get; set; } + public int? ShopkeeperId { get; set; } /// /// Id zbroi /// @@ -20,5 +20,9 @@ namespace SessionCompanion.ViewModels.ShopkeeperItemsViewModels /// Id broni /// public int? WeaponId { get; set; } + /// + /// Ilość przedmiotu + /// + public int Amount { get; set; } } } diff --git a/SessionCompanion/SessionCompanion.ViewModels/ShopkeeperViewModels/ShopkeeperWithItemsViewModel.cs b/SessionCompanion/SessionCompanion.ViewModels/ShopkeeperViewModels/ShopkeeperWithItemsViewModel.cs new file mode 100644 index 0000000..bf965b2 --- /dev/null +++ b/SessionCompanion/SessionCompanion.ViewModels/ShopkeeperViewModels/ShopkeeperWithItemsViewModel.cs @@ -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; } + + /// + /// Nazwa sklepikarza + /// + public string Name { get; set; } + /// + /// Status sklepikarza + /// + public bool IsAvailable { get; set; } + /// + /// Lista przedmiotów danego sklepikarza + /// + public IEnumerable Items { get; set; } + } +} From 69e23bf18748db2284ef8e98c5f9ed14b279622e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karol=20G=C3=B3rzy=C5=84ski?= Date: Wed, 20 Jan 2021 20:53:54 +0100 Subject: [PATCH 2/4] SES-153 Added Profile --- .../SessionCompanion.Services/Profiles/ShopkeeperProfile.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/SessionCompanion/SessionCompanion.Services/Profiles/ShopkeeperProfile.cs b/SessionCompanion/SessionCompanion.Services/Profiles/ShopkeeperProfile.cs index 11507b7..b09f4e0 100644 --- a/SessionCompanion/SessionCompanion.Services/Profiles/ShopkeeperProfile.cs +++ b/SessionCompanion/SessionCompanion.Services/Profiles/ShopkeeperProfile.cs @@ -14,6 +14,8 @@ namespace SessionCompanion.Services.Profiles public ShopkeeperProfile() { CreateMap().ReverseMap(); + CreateMap() + .ForMember(vm => vm.Items, conf => conf.MapFrom(items => items.ShopkeeperItems)).ReverseMap(); } } } From 99f177887f9212ed299b1b1d2582f08bc51a1b1e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karol=20G=C3=B3rzy=C5=84ski?= Date: Wed, 20 Jan 2021 20:54:19 +0100 Subject: [PATCH 3/4] SES-153 Add new service, to create new shopkeeper with items --- .../Interfaces/IShopkeeperService.cs | 4 +++- .../Services/ShopkeeperService.cs | 23 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/SessionCompanion/SessionCompanion.Services/Interfaces/IShopkeeperService.cs b/SessionCompanion/SessionCompanion.Services/Interfaces/IShopkeeperService.cs index e58942e..466b48b 100644 --- a/SessionCompanion/SessionCompanion.Services/Interfaces/IShopkeeperService.cs +++ b/SessionCompanion/SessionCompanion.Services/Interfaces/IShopkeeperService.cs @@ -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 { - + Task> CreateNewShopKeeper(ShopkeeperWithItemsViewModel shopkeeperWithItemsViewModel); } } diff --git a/SessionCompanion/SessionCompanion.Services/Services/ShopkeeperService.cs b/SessionCompanion/SessionCompanion.Services/Services/ShopkeeperService.cs index 42ef9a6..2bc553d 100644 --- a/SessionCompanion/SessionCompanion.Services/Services/ShopkeeperService.cs +++ b/SessionCompanion/SessionCompanion.Services/Services/ShopkeeperService.cs @@ -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 repository) : base(mapper, repository) { } + public async Task> 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(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 }; + } + } } } From 42c786b1d0c4942254f61225d28b8d48d3581699 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karol=20G=C3=B3rzy=C5=84ski?= Date: Wed, 20 Jan 2021 20:54:39 +0100 Subject: [PATCH 4/4] SES-153 Add endpoint to create new Shopkeeper with items --- .../SessionCompanion/Controllers/ShopkeeperController.cs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/SessionCompanion/SessionCompanion/Controllers/ShopkeeperController.cs b/SessionCompanion/SessionCompanion/Controllers/ShopkeeperController.cs index ee0ebc6..b68967b 100644 --- a/SessionCompanion/SessionCompanion/Controllers/ShopkeeperController.cs +++ b/SessionCompanion/SessionCompanion/Controllers/ShopkeeperController.cs @@ -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> CreateNewShopKeeper([Required] ShopkeeperWithItemsViewModel shopkeeperWithItemsViewModel) + { + if (!ModelState.IsValid) + return new ErrorResponse() { StatusCode = 500, Message = "Model is invalid" }; + return await _service.CreateNewShopKeeper(shopkeeperWithItemsViewModel); + } } }