public List <CashBookCostItemData> UpdateCostItemData(int userId, object dtoItem, out Notification notification) { notification = new Notification() { Type = NotificationType.Success }; CashBookCostItemData dataItem = ((Newtonsoft.Json.Linq.JObject)dtoItem).ToObject <CashBookCostItemData>(); List <CashBookCostItemData> costItemData = new List <CashBookCostItemData>(); try { using (var context = CreateContext()) { CashBookCostItem dbItem; if (dataItem.CashBookCostItemID > 0) { dbItem = context.CashBookCostItem.FirstOrDefault(o => o.CashBookCostItemID == dataItem.CashBookCostItemID); if (dbItem == null) { notification.Type = NotificationType.Error; notification.Message = "Can not found data!"; return(costItemData); } } else { dbItem = new CashBookCostItem(); context.CashBookCostItem.Add(dbItem); } converter.DTO2DB_CashBookCostItem(dataItem, ref dbItem); dbItem.UpdatedBy = userId; dbItem.UpdatedDate = DateTime.Now; context.SaveChanges(); costItemData = converter.DB2DTO_CashBookCostItem(context.CashBookMng_CashBookCostItem_View.ToList()); } return(costItemData); } catch (Exception ex) { notification.Type = NotificationType.Error; notification.Message = ex.Message; return(costItemData); } }
public bool DeleteCostItemData(int id, out Notification notification) { notification = new Notification() { Type = NotificationType.Success }; try { using (var context = CreateContext()) { CashBookCostItem dbItem = context.CashBookCostItem.FirstOrDefault(o => o.CashBookCostItemID == id); if (dbItem == null) { notification.Type = NotificationType.Error; notification.Message = "Can not found data!"; return(false); } // set null for all item using it. var dbItem2 = context.CashBook.Where(o => o.CashBookCostItemID == dbItem.CashBookCostItemID); foreach (var item in dbItem2) { item.CashBookCostItemID = null; item.CashBookCostItemDetailID = null; } context.CashBookCostItem.Remove(dbItem); context.SaveChanges(); return(true); } } catch (Exception ex) { notification.Type = NotificationType.Error; notification.Message = ex.Message; return(false); } }
public void DTO2DB_CashBookCostItem(DTO.CashBookCostItemData dataItem, ref CashBookCostItem dbItem) { AutoMapper.Mapper.Map <DTO.CashBookCostItemData, CashBookCostItem>(dataItem, dbItem); }