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

Feature/active menu items #256

Merged
merged 3 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace CoffeeCard.Library.Migrations
{
public partial class addActiveToMenuItem : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<bool>(
name: "Active",
schema: "dbo",
table: "MenuItems",
type: "bit",
nullable: false,
defaultValue: true);
}

protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "Active",
schema: "dbo",
table: "MenuItems");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ protected override void BuildModel(ModelBuilder modelBuilder)

SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"), 1L, 1);

b.Property<bool>("Active")
.HasColumnType("bit");

b.Property<string>("Name")
.IsRequired()
.HasColumnType("nvarchar(450)");
Expand Down
10 changes: 9 additions & 1 deletion coffeecard/CoffeeCard.Library/Services/v2/MenuItemService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,22 @@ public async Task<MenuItemResponse> UpdateMenuItemAsync(int id, UpdateMenuItemRe
throw new ConflictException($"Menu item already exists with name {changedMenuItem.Name}");
}

if (!changedMenuItem.Active &&
await _context.Products.AnyAsync(p => p.EligibleMenuItems.Any(menuItem => menuItem.Id == id)))
{
throw new ConflictException("Menu item is in use and cannot be disabled");
}

menuItem.Name = changedMenuItem.Name;
menuItem.Active = changedMenuItem.Active;

await _context.SaveChangesAsync();

var result = new MenuItemResponse
{
Id = id,
Name = menuItem.Name
Name = menuItem.Name,
Active = menuItem.Active
};

return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,12 @@ public class MenuItemResponse
/// <example>Cappuccino</example>
[Required]
public string Name { get; set; }

/// <summary>
/// Whether or not this menu item is active
/// </summary>
/// <value>Menu item active</value>
/// <example>true</example>
public bool Active { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,13 @@ public class UpdateMenuItemRequest
[Required]
[MinLength(1, ErrorMessage = "Name cannot be an empty string")]
public string Name { get; set; }

/// <summary>
/// Gets or sets the updated active status of the product.
/// </summary>
/// <value>Product Active</value>
/// <example>true</example>
[Required]
public bool Active { get; set; }
}
}
7 changes: 7 additions & 0 deletions coffeecard/CoffeeCard.Models/Entities/MenuItem.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.EntityFrameworkCore;

Expand All @@ -18,15 +20,20 @@
/// <summary>
/// The name of this menu item
/// </summary>
public string Name { get; set; }

Check warning on line 23 in coffeecard/CoffeeCard.Models/Entities/MenuItem.cs

View workflow job for this annotation

GitHub Actions / build / Build codebase / Build and test analog-core

Non-nullable property 'Name' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

/// <summary>
/// The product(s) that are eligible to redeem this menu item
/// </summary>
public ICollection<Product> AssociatedProducts { get; set; }

Check warning on line 28 in coffeecard/CoffeeCard.Models/Entities/MenuItem.cs

View workflow job for this annotation

GitHub Actions / build / Build codebase / Build and test analog-core

Non-nullable property 'AssociatedProducts' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

/// <summary>
/// Navigational property for the join table between Menu Items and Products
/// </summary>
public ICollection<MenuItemProduct> MenuItemProducts { get; set; }

Check warning on line 33 in coffeecard/CoffeeCard.Models/Entities/MenuItem.cs

View workflow job for this annotation

GitHub Actions / build / Build codebase / Build and test analog-core

Non-nullable property 'MenuItemProducts' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

/// <summary>
/// Whether or not this menu item is active
/// </summary>
public bool Active { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,14 @@ public async Task<ActionResult<MenuItemResponse>> AddMenuItem(AddMenuItemRequest
/// <summary>
/// Updates a menu item
/// </summary>
/// <param name="id">Menu item id to update</param>
/// <param name="menuItem">Menu item to update</param>
/// <returns>Menu item</returns>
/// <response code="200">Menu item successfully updated</response>
/// <response code="401">Invalid credentials</response>
/// <response code="404">Menu item not found</response>
///
/// <response code="409">Menu item is in use and cannot be disabled</response>
/// <response code="409">Menu item with the same name already exists</response>
[HttpPut("{id}")]
[AuthorizeRoles(UserGroup.Board)]
[ProducesResponseType(typeof(MenuItemResponse), StatusCodes.Status200OK)]
Expand Down
Loading