Пример #1
0
        public List <CashBookPostCostData> UpdatePostCostData(int userId, object dtoItem, out Notification notification)
        {
            notification = new Notification()
            {
                Type = NotificationType.Success
            };

            CashBookPostCostData        dataItem     = ((Newtonsoft.Json.Linq.JObject)dtoItem).ToObject <CashBookPostCostData>();
            List <CashBookPostCostData> postCostData = new List <CashBookPostCostData>();

            try
            {
                using (var context = CreateContext())
                {
                    CashBookPostCost dbItem;

                    if (dataItem.CashBookPostCostID > 0)
                    {
                        dbItem = context.CashBookPostCost.FirstOrDefault(o => o.CashBookPostCostID == dataItem.CashBookPostCostID);

                        if (dbItem == null)
                        {
                            notification.Type    = NotificationType.Error;
                            notification.Message = "Can not found data!";

                            return(postCostData);
                        }
                    }
                    else
                    {
                        dbItem = new CashBookPostCost();
                        context.CashBookPostCost.Add(dbItem);
                    }

                    converter.DTO2DB_CashBookPostCost(dataItem, ref dbItem);
                    dbItem.UpdatedBy   = userId;
                    dbItem.UpdatedDate = DateTime.Now;

                    context.SaveChanges();

                    postCostData = converter.DB2DTO_CashBookPostCost(context.CashBookMng_CashBookPostCost_View.ToList());
                }

                return(postCostData);
            }
            catch (Exception ex)
            {
                notification.Type    = NotificationType.Error;
                notification.Message = ex.Message;
                return(postCostData);
            }
        }
Пример #2
0
        public bool DeletePostCostData(int id, out Notification notification)
        {
            notification = new Notification()
            {
                Type = NotificationType.Success
            };

            try
            {
                using (var context = CreateContext())
                {
                    CashBookPostCost dbItem = context.CashBookPostCost.FirstOrDefault(o => o.CashBookPostCostID == 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.CashBookPostCostID == dbItem.CashBookPostCostID);
                    foreach (var item in dbItem2)
                    {
                        item.CashBookPostCostID       = null;
                        item.CashBookCostItemID       = null;
                        item.CashBookCostItemDetailID = null;
                    }

                    context.CashBookPostCost.Remove(dbItem);
                    context.SaveChanges();

                    return(true);
                }
            }
            catch (Exception ex)
            {
                notification.Type    = NotificationType.Error;
                notification.Message = ex.Message;
                return(false);
            }
        }
Пример #3
0
 public void DTO2DB_CashBookPostCost(DTO.CashBookPostCostData dataItem, ref CashBookPostCost dbItem)
 {
     AutoMapper.Mapper.Map <DTO.CashBookPostCostData, CashBookPostCost>(dataItem, dbItem);
 }