コード例 #1
0
        public async Task <IActionResult> AddItem(IFormCollection request)
        {
            try
            {
                if (string.IsNullOrEmpty(request.Str("navitem")))
                {
                    return(BadRequest(new ResponseHelper("Something went wrong, please try again later", "No nav-item data was sent")));
                }

                NavItem newItem = JsonConvert.DeserializeObject <NavItem>(request.Str("navitem"));

                // Give an order index that places this at the end of the list, for its section
                newItem.OrderIndex = await _Db.NavItems.Where(n => n.Section == newItem.Section).DefaultIfEmpty().MaxAsync(m => m.OrderIndex) + 1;

                await _Db.AddAsync(newItem);

                // Add new dropdowns, if any
                if (newItem.NavItemPages != null)
                {
                    await _Db.AddRangeAsync(newItem.NavItemPages);
                }

                await _Db.SaveChangesAsync();

                return(Ok(newItem.Id));
            }
            catch (Exception ex)
            {
                _Logger.LogError("Error creating new navlink", ex.Message);
                _Logger.LogError(ex.StackTrace);
                return(BadRequest(new ResponseHelper("Something went wrong, please try again later.", ex.Message)));
            }
        }