-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Admin endpoints for menu items (#251)
It is possible to add, update and read menu items as a board member. Board members can also specify menu items on products when adding and updating products. Co-authored-by: Jonas Anker Rasmussen <jonasanker@gmail.com>
- Loading branch information
1 parent
a42d7e1
commit 7b13729
Showing
16 changed files
with
357 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
coffeecard/CoffeeCard.Library/Services/v2/IMenuItemService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using CoffeeCard.Models.DataTransferObjects.v2.Product; | ||
using CoffeeCard.Models.DataTransferObjects.v2.Products; | ||
|
||
namespace CoffeeCard.Library.Services.v2 | ||
{ | ||
public interface IMenuItemService : IDisposable | ||
{ | ||
Task<IEnumerable<MenuItemResponse>> GetAllMenuItemsAsync(); | ||
Task<MenuItemResponse> AddMenuItemAsync(AddMenuItemRequest newMenuItem); | ||
Task<MenuItemResponse> UpdateMenuItemAsync(int menuItemid, UpdateMenuItemRequest changedMenuItem); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
coffeecard/CoffeeCard.Library/Services/v2/MenuItemService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
using System.Collections.Generic; | ||
using System.Data; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using CoffeeCard.Common.Errors; | ||
using CoffeeCard.Library.Persistence; | ||
using CoffeeCard.Models.DataTransferObjects.v2.Product; | ||
using CoffeeCard.Models.DataTransferObjects.v2.Products; | ||
using CoffeeCard.Models.Entities; | ||
using Microsoft.EntityFrameworkCore; | ||
using Serilog; | ||
|
||
namespace CoffeeCard.Library.Services.v2 | ||
{ | ||
public sealed class MenuItemService : IMenuItemService | ||
{ | ||
private readonly CoffeeCardContext _context; | ||
|
||
public MenuItemService(CoffeeCardContext context) | ||
{ | ||
_context = context; | ||
} | ||
|
||
public async Task<IEnumerable<MenuItemResponse>> GetAllMenuItemsAsync() | ||
{ | ||
return await _context.MenuItems | ||
.OrderBy(p => p.Id) | ||
.Select(p => new MenuItemResponse | ||
{ | ||
Id = p.Id, | ||
Name = p.Name | ||
}) | ||
.ToListAsync(); | ||
} | ||
|
||
public async Task<MenuItemResponse> AddMenuItemAsync(AddMenuItemRequest newMenuItem) | ||
{ | ||
var nameExists = await CheckMenuItemNameExists(newMenuItem.Name); | ||
if (nameExists) | ||
{ | ||
throw new ConflictException($"Menu item already exists with name {newMenuItem.Name}"); | ||
} | ||
|
||
var menuItem = new MenuItem() | ||
{ | ||
Name = newMenuItem.Name | ||
}; | ||
|
||
_context.MenuItems.Add(menuItem); | ||
await _context.SaveChangesAsync(); | ||
|
||
var result = new MenuItemResponse | ||
{ | ||
Id = menuItem.Id, | ||
Name = menuItem.Name | ||
}; | ||
|
||
return result; | ||
} | ||
|
||
private async Task<bool> CheckMenuItemNameExists(string name) | ||
{ | ||
return await _context.MenuItems | ||
.AnyAsync(p => p.Name == name); | ||
} | ||
|
||
public async Task<MenuItemResponse> UpdateMenuItemAsync(int id, UpdateMenuItemRequest changedMenuItem) | ||
{ | ||
var menuItem = await _context.MenuItems.FirstOrDefaultAsync(p => p.Id == id); | ||
|
||
if (menuItem == null) | ||
{ | ||
Log.Warning("No menu item was found by Menu Item Id: {Id}", id); | ||
throw new EntityNotFoundException($"No menu item was found by Menu Item Id {id}"); | ||
} | ||
|
||
var nameExists = await CheckMenuItemNameExists(changedMenuItem.Name); | ||
if (nameExists) | ||
{ | ||
throw new ConflictException($"Menu item already exists with name {changedMenuItem.Name}"); | ||
} | ||
|
||
menuItem.Name = changedMenuItem.Name; | ||
|
||
await _context.SaveChangesAsync(); | ||
|
||
var result = new MenuItemResponse | ||
{ | ||
Id = id, | ||
Name = menuItem.Name | ||
}; | ||
|
||
return result; | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
_context?.Dispose(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
coffeecard/CoffeeCard.Models/DataTransferObjects/v2/Product/AddMenuItemRequest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using System.Collections.Generic; | ||
using System.ComponentModel; | ||
using System.ComponentModel.DataAnnotations; | ||
using CoffeeCard.Models.Entities; | ||
|
||
namespace CoffeeCard.Models.DataTransferObjects.v2.Product | ||
{ | ||
/// <summary> | ||
/// Initiate a new menuitem add request. | ||
/// </summary> | ||
public class AddMenuItemRequest | ||
{ | ||
/// <summary> | ||
/// Gets or sets the name of the product. | ||
/// </summary> | ||
/// <value>Product Name</value> | ||
/// <example>Latte</example> | ||
[Required] | ||
[MinLength(1, ErrorMessage = "Name cannot be an empty string")] | ||
public string Name { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.