public async Task <IActionResult> InsertItem(Guid employeeId, [FromBody] ItemModel item) { #region serverside validation if (String.IsNullOrWhiteSpace(item.name)) { return(BadRequest("Name can't be empty")); } if (item.name.Length > 50) { return(BadRequest("Name shouldn't exceed 50 characters")); } if (String.IsNullOrWhiteSpace(item.description)) { return(BadRequest("Description can't be empty")); } if (item.description.Length > 2000) { return(BadRequest("Description shouldn't exceed 2000 characters")); } #endregion Item newItem = new Item(); try { //newItem.ItemId generated by default newItem.Name = item.name; // not null newItem.Description = item.description; // not null newItem.BrandId = item.brandId; // not null newItem.CategoryId = item.categoryId; // not null //kali newItem.GenderId = item.genderId; // not null newItem.Price = item.price; // not null newItem.IsActive = false; context.Item.Add(newItem); await context.SaveChangesAsync(); #region AdminLog AdminLog adminLog = new AdminLog(); adminLog.TableName = "Item"; adminLog.FieldName = "ItemId"; adminLog.FieldValue = newItem.ItemId.ToString(); adminLog.AdminLogActionId = (int)AdminLogActionEnum.Create; adminLog.Notes = null; adminLog.EmployeeId = employeeId; adminLog.Timestamp = DateTime.Now; analyticsRepo.InsertAdminLog(adminLog); #endregion return(Ok(newItem.ItemId)); } catch (Exception ex) { #region AdminLog ErrorLog errorLog = new ErrorLog(); errorLog.Namespace = "PapaJohnsCloneApi.Controllers"; errorLog.Class = "AnalyticsController"; errorLog.Method = "InsertItem"; errorLog.MethodParams = "(Guid employeeId, [FromBody] ItemModel item)"; errorLog.ErrorLogTypeId = (int)ErrorLogTypeEnum.Error; errorLog.ShortComment = "Failed to insert into Item"; errorLog.DetailedComment = ex.InnerException.Message; errorLog.Exception = XmlManipulation.ConvertObjectToXml <Exception>(ex.InnerException); errorLog.EmployeeId = employeeId; errorLog.Timestamp = DateTime.Now; // clean Exception before saving AdminLog in db context.Entry(newItem).State = EntityState.Detached; analyticsRepo.InsertErrorLog(errorLog); #endregion return(BadRequest()); } }