Skip to content

Commit

Permalink
Feature/active menu items (#256)
Browse files Browse the repository at this point in the history
  • Loading branch information
fredpetersen authored Feb 13, 2024
1 parent 84849d2 commit 7aa6c9b
Show file tree
Hide file tree
Showing 8 changed files with 715 additions and 2 deletions.

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
11 changes: 10 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,23 @@ public async Task<MenuItemResponse> UpdateMenuItemAsync(int id, UpdateMenuItemRe
throw new ConflictException($"Menu item already exists with name {changedMenuItem.Name}");
}


var menuItemIsUsed = await _context.Products.AnyAsync(p => p.EligibleMenuItems.Any(menuItem => menuItem.Id == id));
if (!changedMenuItem.Active && menuItemIsUsed)
{
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 Down Expand Up @@ -29,4 +31,9 @@ public class MenuItem
/// 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 / dev-deploy / Build codebase / 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

0 comments on commit 7aa6c9b

Please sign in to comment.