/// <summary> /// Creates an item to our persistent database. Returns true if affected record count is gt 0. /// </summary> /// <param name="item"></param> /// <returns></returns> private async Task <bool> CreateItem(ViewModels.ContentItemModel item) { // Creating an entity here so we cast the enum to it's data type so our datastore can process. Db.Items.Add(new Business.Models.ContentItem() { CategoryId = (int)item.Category, Name = item.Name, Cost = item.Cost }); return(await Db.SaveChangesAsync() > 0); }
public async Task <IActionResult> Create(ViewModels.ContentItemModel model) { // I do my validations up front so I know after this line we are good to go, no application issue should occur after this. // if something breaks it is infrastructure (memory, db connection etc). var createItemValidation = new Business.ItemValidator().CreateItemIsValid(new Business.Models.ContentItem() { CategoryId = (int)model.Category }); if (!createItemValidation.IsValid) { this.AddMessage(MessageType.Error, createItemValidation.Message); return(RedirectToAction("Index")); } var isItemCreated = await CreateItem(model); if (isItemCreated) { this.AddMessage(MessageType.Success, string.Format(Business.LibBus.SUCCESS_ITEM_CREATED, model.Name, model.Category)); } return(RedirectToAction("Index")); }