Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changes to products API #253

Merged
merged 7 commits into from
Feb 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions coffeecard/CoffeeCard.Library/Services/v2/IProductService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using CoffeeCard.Models.DataTransferObjects.v2.Product;
using CoffeeCard.Models.DataTransferObjects.v2.Products;
using CoffeeCard.Models.Entities;

namespace CoffeeCard.Library.Services.v2
{
public interface IProductService : IDisposable
{
Task<IEnumerable<Product>> GetProductsForUserAsync(User user);
Task<Product> GetProductAsync(int productId);
Task<IEnumerable<Product>> GetAllProductsAsync();
Task<ChangedProductResponse> AddProduct(AddProductRequest product);

Task<ChangedProductResponse> UpdateProduct(UpdateProductRequest product);
Task<IEnumerable<ProductResponse>> GetProductsForUserAsync(User user);
Task<ProductResponse> GetProductAsync(int productId);
Task<IEnumerable<ProductResponse>> GetAllProductsAsync();
Task<ProductResponse> AddProduct(AddProductRequest product);
Task<ProductResponse> UpdateProduct(int productId, UpdateProductRequest product);
}
}
60 changes: 17 additions & 43 deletions coffeecard/CoffeeCard.Library/Services/v2/ProductService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using CoffeeCard.Models.Entities;
using Microsoft.EntityFrameworkCore;
using Serilog;
using CoffeeCard.Library.Utils;

namespace CoffeeCard.Library.Services.v2
{
Expand All @@ -21,31 +22,33 @@ public ProductService(CoffeeCardContext context)
_context = context;
}

public async Task<IEnumerable<Product>> GetProductsForUserAsync(User user)
public async Task<IEnumerable<ProductResponse>> GetProductsForUserAsync(User user)
{
return await GetProductsAsync(user.UserGroup);
}

private async Task<IEnumerable<Product>> GetProductsAsync(UserGroup userGroup)
private async Task<IEnumerable<ProductResponse>> GetProductsAsync(UserGroup userGroup)
{
return await _context.Products
.Where(p => p.ProductUserGroup.Any(pug => pug.UserGroup == userGroup))
.Where(p => p.Visible).OrderBy(p => p.Id)
.Include(p => p.ProductUserGroup)
.Include(p => p.EligibleMenuItems)
.Select(p => p.ToProductResponse())
.ToListAsync();
}

public async Task<IEnumerable<Product>> GetAllProductsAsync()
public async Task<IEnumerable<ProductResponse>> GetAllProductsAsync()
{
return await _context.Products
.OrderBy(p => p.Id)
.Include(p => p.ProductUserGroup)
.Include(p => p.EligibleMenuItems)
.Select(p => p.ToProductResponse())
.ToListAsync();
}

public async Task<Product> GetProductAsync(int productId)
public async Task<ProductResponse> GetProductAsync(int productId)
{
var product = await _context.Products
.Include(p => p.ProductUserGroup)
Expand All @@ -58,7 +61,7 @@ public async Task<Product> GetProductAsync(int productId)
throw new EntityNotFoundException($"No product was found by Product Id: {productId}");
}

return product;
return product.ToProductResponse();
}

private async Task<bool> CheckProductUniquenessAsync(string name, int price)
Expand All @@ -69,7 +72,7 @@ private async Task<bool> CheckProductUniquenessAsync(string name, int price)
return product == null;
}

public async Task<ChangedProductResponse> AddProduct(AddProductRequest newProduct)
public async Task<ProductResponse> AddProduct(AddProductRequest newProduct)
{
var unique = await CheckProductUniquenessAsync(newProduct.Name, newProduct.Price);
if (!unique)
Expand Down Expand Up @@ -99,28 +102,15 @@ public async Task<ChangedProductResponse> AddProduct(AddProductRequest newProduc
_context.Products.Add(product);
await _context.SaveChangesAsync();

var result = new ChangedProductResponse
{
Price = product.Price,
Description = product.Description,
Name = product.Name,
NumberOfTickets = product.NumberOfTickets,
Visible = product.Visible,
AllowedUserGroups = newProduct.AllowedUserGroups,
MenuItems = product.EligibleMenuItems
.Select(mi => new MenuItemResponse
{
Id = mi.Id,
Name = mi.Name
})
};

return result;
return product.ToProductResponse();
}

public async Task<ChangedProductResponse> UpdateProduct(UpdateProductRequest changedProduct)
public async Task<ProductResponse> UpdateProduct(int productId, UpdateProductRequest changedProduct)
{
var product = await GetProductAsync(changedProduct.Id);
var product = await _context.Products
.Include(p => p.ProductUserGroup)
.Include(p => p.EligibleMenuItems)
.FirstOrDefaultAsync(p => p.Id == productId);

product.Price = changedProduct.Price;
product.Description = changedProduct.Description;
Expand All @@ -130,7 +120,7 @@ public async Task<ChangedProductResponse> UpdateProduct(UpdateProductRequest cha
product.ProductUserGroup = changedProduct.AllowedUserGroups
.Select(userGroup => new ProductUserGroup
{
ProductId = changedProduct.Id,
ProductId = product.Id,
UserGroup = userGroup
})
.ToList();
Expand All @@ -141,23 +131,7 @@ public async Task<ChangedProductResponse> UpdateProduct(UpdateProductRequest cha

await _context.SaveChangesAsync();

var result = new ChangedProductResponse
{
Price = product.Price,
Description = product.Description,
Name = product.Name,
NumberOfTickets = product.NumberOfTickets,
Visible = product.Visible,
AllowedUserGroups = changedProduct.AllowedUserGroups,
MenuItems = product.EligibleMenuItems
.Select(item => new MenuItemResponse
{
Id = item.Id,
Name = item.Name
})
};

return result;
return product.ToProductResponse();
}

public void Dispose()
Expand Down
7 changes: 4 additions & 3 deletions coffeecard/CoffeeCard.Library/Services/v2/PurchaseService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using CoffeeCard.Library.Persistence;
using CoffeeCard.MobilePay.Service.v2;
using CoffeeCard.Models.DataTransferObjects.v2.MobilePay;
using CoffeeCard.Models.DataTransferObjects.v2.Products;
using CoffeeCard.Models.DataTransferObjects.v2.Purchase;
using CoffeeCard.Models.Entities;
using Microsoft.AspNetCore.Http;
Expand Down Expand Up @@ -72,10 +73,10 @@ public async Task<InitiatePurchaseResponse> InitiatePurchase(InitiatePurchaseReq
/// <param name="product">Product</param>
/// <exception cref="IllegalUserOperationException">User is not entitled to purchase product</exception>
/// <exception cref="ArgumentException">PaymentType FreePurchase used for a non-free product</exception>
private static void CheckUserIsAllowedToPurchaseProduct(User user, InitiatePurchaseRequest initiateRequest, Product product)
private static void CheckUserIsAllowedToPurchaseProduct(User user, InitiatePurchaseRequest initiateRequest, ProductResponse product)
{
//Product does not belong to same userGroup as user
if (!product.ProductUserGroup.Any(pug => pug.UserGroup == user.UserGroup))
if (!product.AllowedUserGroups.Any(ug => ug == user.UserGroup))
{
Log.Warning(
"User {UserId} is not authorized to purchase Product Id: {ProductId} as user is not in Product User Group",
Expand All @@ -92,7 +93,7 @@ private static void CheckUserIsAllowedToPurchaseProduct(User user, InitiatePurch
}
}

private async Task<(Purchase purchase, PaymentDetails paymentDetails)> InitiatePaymentAsync(InitiatePurchaseRequest purchaseRequest, Product product, User user)
private async Task<(Purchase purchase, PaymentDetails paymentDetails)> InitiatePaymentAsync(InitiatePurchaseRequest purchaseRequest, ProductResponse product, User user)
{
var orderId = await GenerateUniqueOrderId();
PaymentDetails paymentDetails;
Expand Down
19 changes: 19 additions & 0 deletions coffeecard/CoffeeCard.Library/Utils/ProductExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Linq;
using CoffeeCard.Models.DataTransferObjects.v2.Products;
using CoffeeCard.Models.Entities;

namespace CoffeeCard.Library.Utils
Expand All @@ -21,5 +22,23 @@ public static bool IsPerk(this Product product)

return product.ProductUserGroup.All(pug => pug.UserGroup != UserGroup.Customer);
}

public static ProductResponse ToProductResponse(this Product product)
{
return new ProductResponse
{
Id = product.Id,
Name = product.Name,
Description = product.Description,
NumberOfTickets = product.NumberOfTickets,
Price = product.Price,
IsPerk = product.IsPerk(),
Visible = product.Visible,
AllowedUserGroups = product.ProductUserGroup.Select(pug => pug.UserGroup),
EligibleMenuItems = product.EligibleMenuItems.Select(
mi => new MenuItemResponse { Id = mi.Id, Name = mi.Name }
)
};
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,6 @@ namespace CoffeeCard.Models.DataTransferObjects.v2.Product
/// </summary>
public class UpdateProductRequest
{
/// <summary>
/// Gets or sets the ID of the product to update.
/// </summary>
/// <value>Product Id</value>
/// <example>1</example>
[Required]
public int Id { get; set; }

/// <summary>
/// Gets or sets the updated price of the product.
/// </summary>
Expand Down
Loading
Loading