created edit MenuItem and subMenuItem end -points
This commit is contained in:
parent
cc5badeb0f
commit
5988a98016
@ -1,14 +1,14 @@
|
||||
|
||||
using Marco.Pms.Model.AppMenu;
|
||||
using Marco.Pms.Model.AppMenu;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using MongoDB.Driver;
|
||||
using MongoDB.Bson;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Marco.Pms.CacheHelper
|
||||
{
|
||||
|
||||
public class SideBarMenu
|
||||
{
|
||||
private readonly IMongoCollection<MenuSection> _collection;
|
||||
@ -31,15 +31,88 @@ namespace Marco.Pms.CacheHelper
|
||||
await _collection.InsertOneAsync(section);
|
||||
return section;
|
||||
}
|
||||
catch(Exception ex)
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error occured while added in mongo");
|
||||
_logger.LogError(ex, "Error occurred while adding MenuSection.");
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// You can add Get, Update, Delete later here
|
||||
}
|
||||
public async Task<MenuItem?> UpdateMenuItemAsync(Guid sectionId, Guid itemId, MenuItem updatedItem)
|
||||
{
|
||||
try
|
||||
{
|
||||
var filter = Builders<MenuSection>.Filter.And(
|
||||
Builders<MenuSection>.Filter.Eq(s => s.Id, sectionId),
|
||||
Builders<MenuSection>.Filter.ElemMatch(s => s.Items, i => i.Id == itemId)
|
||||
);
|
||||
|
||||
}
|
||||
var update = Builders<MenuSection>.Update
|
||||
.Set("Items.$.Text", updatedItem.Text)
|
||||
.Set("Items.$.Icon", updatedItem.Icon)
|
||||
.Set("Items.$.Available", updatedItem.Available)
|
||||
.Set("Items.$.Link", updatedItem.Link);
|
||||
|
||||
var result = await _collection.UpdateOneAsync(filter, update);
|
||||
if (result.ModifiedCount > 0)
|
||||
{
|
||||
// Re-fetch section and return the updated item
|
||||
var section = await _collection.Find(s => s.Id == sectionId).FirstOrDefaultAsync();
|
||||
return section?.Items.FirstOrDefault(i => i.Id == itemId);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error updating MenuItem.");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public async Task<SubMenuItem?> UpdateSubmenuItemAsync(Guid sectionId, Guid itemId, Guid subItemId, SubMenuItem updatedSub)
|
||||
{
|
||||
try
|
||||
{
|
||||
var filter = Builders<MenuSection>.Filter.Eq(s => s.Id, sectionId);
|
||||
|
||||
var arrayFilters = new List<ArrayFilterDefinition>
|
||||
{
|
||||
new BsonDocumentArrayFilterDefinition<BsonDocument>(
|
||||
new BsonDocument("item._id", itemId.ToString())),
|
||||
new BsonDocumentArrayFilterDefinition<BsonDocument>(
|
||||
new BsonDocument("sub._id", subItemId.ToString()))
|
||||
};
|
||||
|
||||
var update = Builders<MenuSection>.Update
|
||||
.Set("Items.$[item].Submenu.$[sub].Text", updatedSub.Text)
|
||||
.Set("Items.$[item].Submenu.$[sub].Available", updatedSub.Available)
|
||||
.Set("Items.$[item].Submenu.$[sub].Link", updatedSub.Link)
|
||||
.Set("Items.$[item].Submenu.$[sub].PermissionKey", updatedSub.PermissionKey);
|
||||
|
||||
var options = new UpdateOptions { ArrayFilters = arrayFilters };
|
||||
|
||||
var result = await _collection.UpdateOneAsync(filter, update, options);
|
||||
|
||||
if (result.ModifiedCount == 0)
|
||||
return null;
|
||||
|
||||
var updatedSection = await _collection.Find(x => x.Id == sectionId).FirstOrDefaultAsync();
|
||||
|
||||
var subItem = updatedSection?.Items
|
||||
.FirstOrDefault(i => i.Id.ToString() == itemId.ToString())?
|
||||
.Submenu
|
||||
.FirstOrDefault(s => s.Id.ToString() == subItemId.ToString());
|
||||
|
||||
return subItem;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error updating SubMenuItem.");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -13,6 +13,7 @@ using MarcoBMS.Services.Service;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Http.HttpResults;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using MongoDB.Driver;
|
||||
using Org.BouncyCastle.Asn1.Ocsp;
|
||||
using System.Threading.Tasks;
|
||||
using static System.Collections.Specialized.BitVector32;
|
||||
@ -51,8 +52,8 @@ namespace Marco.Pms.Services.Controllers
|
||||
//}
|
||||
|
||||
|
||||
[HttpPost("create/appsidebar")]
|
||||
public async Task<IActionResult> PostAppSideBarMenu([FromBody] MenuSectionDto sidebarMenu)
|
||||
[HttpPost("sidebar/menusection")]
|
||||
public async Task<IActionResult> CreateAppSideBarMenu([FromBody] MenuSectionDto MenuSecetion)
|
||||
{
|
||||
|
||||
|
||||
@ -64,10 +65,10 @@ namespace Marco.Pms.Services.Controllers
|
||||
return StatusCode(403, ApiResponse<object>.ErrorResponse("access denied", "User haven't permission", 403));
|
||||
}
|
||||
|
||||
var sideMenu = _mapper.Map<MenuSection>(sidebarMenu);
|
||||
var sideMenuSection = _mapper.Map<MenuSection>(MenuSecetion);
|
||||
try
|
||||
{
|
||||
sideMenu = await _sideBarMenuHelper.CreateMenuSectionAsync(sideMenu);
|
||||
sideMenuSection = await _sideBarMenuHelper.CreateMenuSectionAsync(sideMenuSection);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@ -75,23 +76,87 @@ namespace Marco.Pms.Services.Controllers
|
||||
return StatusCode(500, ApiResponse<object>.ErrorResponse("Server Error", ex, 500));
|
||||
}
|
||||
|
||||
if (sideMenu == null) {
|
||||
if (sideMenuSection == null) {
|
||||
_logger.LogWarning("Error Occurred while creating Menu");
|
||||
return BadRequest(ApiResponse<object>.ErrorResponse("Menu creation failed", 400));
|
||||
return BadRequest(ApiResponse<object>.ErrorResponse("Invalid MenuSection", 400));
|
||||
}
|
||||
|
||||
_logger.LogInfo("Error Occurred while creating Menu");
|
||||
return Ok(ApiResponse<object>.SuccessResponse(sideMenu, "Sidebar menu created successfully.", 201));
|
||||
return Ok(ApiResponse<object>.SuccessResponse(sideMenuSection, "Sidebar menu created successfully.", 201));
|
||||
|
||||
}
|
||||
|
||||
[HttpPut("sidebar/{sectionId}/items/{itemId}")]
|
||||
public async Task<IActionResult> UpdateMenuItem(Guid sectionId, Guid itemId, [FromBody] MenuItemDto updatedMenuItem)
|
||||
{
|
||||
|
||||
if (sectionId == Guid.Empty || itemId == Guid.Empty || updatedMenuItem == null)
|
||||
{
|
||||
_logger.LogWarning("Error Occurred while creating Menu");
|
||||
return BadRequest(ApiResponse<object>.ErrorResponse("Invalid section ID, item ID, or menu item payload.", 400));
|
||||
|
||||
}
|
||||
|
||||
var sideMenuItem = _mapper.Map<MenuItem>(updatedMenuItem);
|
||||
|
||||
try
|
||||
{
|
||||
|
||||
sideMenuItem = await _sideBarMenuHelper.UpdateMenuItemAsync(sectionId, itemId, sideMenuItem);
|
||||
|
||||
if (sideMenuItem == null)
|
||||
{
|
||||
_logger.LogWarning("Error Occurred while Updating SidBar Section:{SectionId} MenuItem:{itemId} ");
|
||||
return BadRequest(ApiResponse<object>.ErrorResponse("Menu creation failed", 400));
|
||||
}
|
||||
|
||||
_logger.LogInfo("SidBar Section{SectionId} MenuItem {itemId} Updated ");
|
||||
return Ok(ApiResponse<object>.SuccessResponse(sideMenuItem, "Sidebar MenuItem Updated successfully.", 201));
|
||||
|
||||
}
|
||||
catch (Exception ex) {
|
||||
_logger.LogError(ex, "Error Occurred while creating MenuItem");
|
||||
return StatusCode(500, ApiResponse<object>.ErrorResponse("Server Error", ex, 500));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
[HttpPut("sidebar/{sectionId}/items/{itemId}/subitems/{subItemId}")]
|
||||
public async Task<IActionResult> UpdateSubmenuItem(Guid sectionId,Guid itemId,Guid subItemId,[FromBody] SubMenuItemDto updatedSubMenuItem)
|
||||
{
|
||||
if (sectionId == Guid.Empty || itemId == Guid.Empty || subItemId == Guid.Empty || updatedSubMenuItem == null)
|
||||
return BadRequest(ApiResponse<object>.ErrorResponse("Invalid input", 400));
|
||||
|
||||
try
|
||||
{
|
||||
var SubMenuItem = _mapper.Map<SubMenuItem>(updatedSubMenuItem);
|
||||
SubMenuItem = await _sideBarMenuHelper.UpdateSubmenuItemAsync(sectionId, itemId, subItemId, SubMenuItem);
|
||||
|
||||
|
||||
if (SubMenuItem == null)
|
||||
return NotFound(ApiResponse<object>.ErrorResponse("Submenu item not found", 404));
|
||||
|
||||
_logger.LogInfo("SidBar Section{SectionId} MenuItem {itemId} SubMenuItem {subItemId} Updated");
|
||||
return Ok(ApiResponse<object>.SuccessResponse(SubMenuItem, "Submenu item updated successfully"));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error Occurred while Updating Sub-MenuItem");
|
||||
return StatusCode(500, ApiResponse<object>.ErrorResponse("Server Error", ex, 500));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user