From 071d3fab8b6f7ef04cefd4a4410f128396f714b9 Mon Sep 17 00:00:00 2001 From: A-Guldborg Date: Mon, 27 Nov 2023 23:25:16 +0100 Subject: [PATCH] Small fixes to decrease unnecessary code --- .../Services/v2/ProductService.cs | 24 ++++++++----------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/coffeecard/CoffeeCard.Library/Services/v2/ProductService.cs b/coffeecard/CoffeeCard.Library/Services/v2/ProductService.cs index 42edd6c0..e1e471ce 100644 --- a/coffeecard/CoffeeCard.Library/Services/v2/ProductService.cs +++ b/coffeecard/CoffeeCard.Library/Services/v2/ProductService.cs @@ -78,15 +78,13 @@ public async Task AddProduct(AddProductRequest newProduc Name = newProduct.Name, NumberOfTickets = newProduct.NumberOfTickets, ExperienceWorth = 0, - Visible = newProduct.Visible + Visible = newProduct.Visible, + ProductUserGroup = newProduct.AllowedUserGroups.Select(userGroup => new ProductUserGroup + { + UserGroup = userGroup + }).ToList() }; - product.ProductUserGroup = newProduct.AllowedUserGroups.Select(userGroup => new ProductUserGroup - { - ProductId = product.Id, - UserGroup = userGroup - }).ToList(); - _context.Products.Add(product); await _context.SaveChangesAsync(); @@ -105,19 +103,17 @@ public async Task AddProduct(AddProductRequest newProduc public async Task UpdateProduct(UpdateProductRequest changedProduct) { - var newProductUserGroups = changedProduct.AllowedUserGroups.Select(userGroup => new ProductUserGroup - { - ProductId = changedProduct.Id, - UserGroup = userGroup - }).ToList(); - var product = await GetProductAsync(changedProduct.Id); product.Price = changedProduct.Price; product.Description = changedProduct.Description; product.NumberOfTickets = changedProduct.NumberOfTickets; product.Name = changedProduct.Name; product.Visible = changedProduct.Visible; - product.ProductUserGroup = newProductUserGroups; + product.ProductUserGroup = changedProduct.AllowedUserGroups.Select(userGroup => new ProductUserGroup + { + ProductId = changedProduct.Id, + UserGroup = userGroup + }).ToList(); await _context.SaveChangesAsync();