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

Cleanup Products API v2 endpoints #224

Closed
wants to merge 3 commits into from
Closed
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
6 changes: 3 additions & 3 deletions coffeecard/CoffeeCard.Library/Services/v2/IProductService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
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
Expand All @@ -11,8 +12,7 @@ public interface IProductService : IDisposable
Task<IEnumerable<Product>> GetPublicProductsAsync();
Task<IEnumerable<Product>> GetProductsForUserAsync(User user);
Task<Product> GetProductAsync(int productId);
Task<ChangedProductResponse> AddProduct(AddProductRequest product);

Task<ChangedProductResponse> UpdateProduct(UpdateProductRequest product);
Task<DetailedProductResponse> AddProduct(AddProductRequest product);
Task<DetailedProductResponse> UpdateProduct(int productId, UpdateProductRequest updateProduct);
}
}
26 changes: 13 additions & 13 deletions coffeecard/CoffeeCard.Library/Services/v2/ProductService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ private async Task<bool> CheckProductUniquenessAsync(string name, int price)
return product == null;
}

public async Task<ChangedProductResponse> AddProduct(AddProductRequest newProduct)
public async Task<DetailedProductResponse> AddProduct(AddProductRequest newProduct)
{
var unique = await CheckProductUniquenessAsync(newProduct.Name, newProduct.Price);
if (!unique)
Expand Down Expand Up @@ -91,13 +91,11 @@ public async Task<ChangedProductResponse> AddProduct(AddProductRequest newProduc
}).ToList();

_context.ProductUserGroups.AddRange(productUserGroups);


await _context.SaveChangesAsync();


var result = new ChangedProductResponse
var result = new DetailedProductResponse
{
Id = product.Id,
Price = product.Price,
Description = product.Description,
Name = product.Name,
Expand All @@ -108,19 +106,21 @@ public async Task<ChangedProductResponse> AddProduct(AddProductRequest newProduc
return result;
}

public async Task<ChangedProductResponse> UpdateProduct(UpdateProductRequest changedProduct)
public async Task<DetailedProductResponse> UpdateProduct(int productId, UpdateProductRequest updateProduct)
{
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;
var product = await GetProductAsync(productId);

product.Price = updateProduct.Price;
product.Description = updateProduct.Description;
product.NumberOfTickets = updateProduct.NumberOfTickets;
product.Name = updateProduct.Name;
product.Visible = updateProduct.Visible;

await _context.SaveChangesAsync();

var result = new ChangedProductResponse
var result = new DetailedProductResponse
{
Id = product.Id,
Price = product.Price,
Description = product.Description,
Name = product.Name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ namespace CoffeeCard.Models.DataTransferObjects.v2.Product
/// </summary>
/// <example>
/// {
/// "Name": "Latte",
/// "Price": 25,
/// "NumberOfTickets": 10,
/// "Description": "xxx",
/// "Visible": true
/// "name": "Latte",
/// "price": 25,
/// "numberOfTickets": 10,
/// "description": "Milkbased espresso drink",
/// "visible": true,
/// "allowedUserGroups": ["Customer"]
/// }
/// </example>
public class AddProductRequest
Expand All @@ -23,46 +24,51 @@ public class AddProductRequest
/// Gets or sets the price of the product.
/// </summary>
/// <value>Product Price</value>
/// <example> 10 </example>
/// <example>10</example>
[Required]
[Range(0, int.MaxValue, ErrorMessage = "Price must be a non-negative integer.")]
public int Price { get; set; }

/// <summary>
/// Gets or sets the number of tickets associated with the product.
/// </summary>
/// <value> Number of tickets associated with a product </value>
/// <example> 5 </example>
/// <value>Number of tickets associated with a product</value>
/// <example>5</example>
[Required]
[Range(0, int.MaxValue, ErrorMessage = "Number of Tickets must be a non-negative integer.")]
public int NumberOfTickets { get; set; }

/// <summary>
/// Gets or sets the name of the product.
/// </summary>
/// <value> Product Name </value>
/// <example> Latte </example>
/// <value>Product Name</value>
/// <example>Latte</example>
[Required]
[MinLength(1, ErrorMessage = "Name cannot be an empty string.")]
public string Name { get; set; }

/// <summary>
/// Gets or sets the description of the product.
/// </summary>
/// <value> Product Description </value>
/// <example> A homemade latte with soy milk </example>
/// <value>Product Description</value>
/// <example>A homemade latte with soy milk</example>
[Required]
[MinLength(1, ErrorMessage = "Description cannot be an empty string.")]
public string Description { get; set; }

/// <summary>
/// Gets or sets the visibility of the product. Default is true.
/// </summary>
/// <value> Product Visibility </value>
/// <example> true </example>
/// <value>Product Visibility</value>
/// <example>true</example>
[DefaultValue(true)]
public bool Visible { get; set; } = true;


/// <summary>
/// List of UserGroups who are entitled to purchase the product
/// </summary>
/// <example>["Customer"]</example>
[Required]
public IEnumerable<UserGroup> AllowedUserGroups { get; set; }

Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,34 @@
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;

namespace CoffeeCard.Models.DataTransferObjects.v2.Product
{
/// <summary>
/// Represents the product response.
/// </summary>
public class ChangedProductResponse
/// <example>
/// {
/// "id": 1,
/// "price": 150,
/// "numberOfTickets": 10,
/// "name": "Espresso",
/// "description": "A homemade espresso from fresh beans",
/// "visible": true
/// }
/// </example>
public class DetailedProductResponse
{
/// <summary>
/// Product Id
/// </summary>
/// <value>ProductId</value>
/// <example>1</example>
[Required]
public int Id { get; set; }

/// <summary>
/// Gets or sets the price of the product.
/// </summary>
/// <example> 150 </example>
/// <example>150</example>
[Required]
[Range(0, int.MaxValue, ErrorMessage = "Price must be a non-negative integer.")]
public int Price { get; set; }
Expand All @@ -21,34 +37,34 @@ public class ChangedProductResponse
/// Gets or sets the number of tickets associated with the product.
/// </summary>
/// <value> Number of Tickets of a Product </value>
/// <example> 5 </example>
/// <example>5</example>
[Required]
[Range(0, int.MaxValue, ErrorMessage = "Number of tickets must be a non-negative integer.")]
public int NumberOfTickets { get; set; }

/// <summary>
/// Gets or sets the name of the product.
/// </summary>
/// <value> Product Name </value>
/// <example> Espresso </example>
/// <value>Product Name</value>
/// <example>Espresso</example>
[Required]
[MinLength(1, ErrorMessage = "Name cannot be an empty string.")]
public string Name { get; set; }

/// <summary>
/// Gets or sets the description of the product.
/// </summary>
/// <value> Product Description </value>
/// <example> A homemade espresso from fresh beans </example>
/// <value>Product Description</value>
/// <example>A homemade espresso from fresh beans</example>
[Required]
[MinLength(1, ErrorMessage = "Description cannot be an empty string.")]
public string Description { get; set; }

/// <summary>
/// Gets or sets the visibility of the product.
/// </summary>
/// <value> Product Visibility </value>
/// <example> true </example>
/// <value>Product Visibility</value>
/// <example>true</example>
[Required]
public bool Visible { get; set; }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,65 +8,56 @@ namespace CoffeeCard.Models.DataTransferObjects.v2.Product
/// </summary>
/// <example>
/// {
/// "Id": 1,
/// "Price": 150,
/// "NumberOfTickets": 10,
/// "Name": "Espresso",
/// "Description": "A coffee made by forcing steam through ground coffee beans.",
/// "Visible": false
/// "price": 150,
/// "numberOfTickets": 10,
/// "name": "Espresso",
/// "description": "A coffee made by forcing steam through ground coffee beans.",
/// "visible": false
/// }
/// </example>
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>
/// <value> Product Price </value>
/// <example> 10 </example>
/// <value>Product Price</value>
/// <example>10</example>
[Required]
[Range(0, int.MaxValue, ErrorMessage = "Price must be a non-negative integer.")]
public int Price { get; set; }

/// <summary>
/// Gets or sets the updated number of tickets associated with the product.
/// </summary>
/// <value> Number of Tickets of a Product </value>
/// <example> 5 </example>
/// <value>Number of Tickets of a Product</value>
/// <example>5</example>
[Required]
[Range(0, int.MaxValue, ErrorMessage = "Number of Tickets must be a non-negative integer.")]
public int NumberOfTickets { get; set; }

/// <summary>
/// Gets or sets the updated name of the product.
/// </summary>
/// <value> Product Name </value>
/// <example> Espresso </example>
/// <value>Product Name</value>
/// <example>Espresso</example>
[Required]
[MinLength(1, ErrorMessage = "Name cannot be an empty string.")]
public string Name { get; set; }

/// <summary>
/// Gets or sets the updated description of the product.
/// </summary>
/// <value> Product Description </value>
/// <example> A homemade espresso from fresh beans </example>
/// <value>Product Description</value>
/// <example>A homemade espresso from fresh beans</example>
[Required]
[MinLength(1, ErrorMessage = "Description cannot be an empty string.")]
public string Description { get; set; }

/// <summary>
/// Gets or sets the updated visibility of the product. Default is true.
/// </summary>
/// <value> Product Visibility </value>
/// <example> true </example>
/// <value>Product Visibility</value>
/// <example>true</example>
[DefaultValue(true)]
public bool Visible { get; set; } = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace CoffeeCard.Models.DataTransferObjects.v2.Products
/// "isPerk": true
/// }
/// </example>
public class ProductResponse
public class SimpleProductResponse
{
/// <summary>
/// Id of product
Expand Down
Loading
Loading