Esempio n. 1
0
        public override bool UpdateData(int userId, int id, ref object dtoItem, out Notification notification)
        {
            DTO.PriceListClientChargeDto dtoConvert = ((Newtonsoft.Json.Linq.JObject)dtoItem).ToObject <DTO.PriceListClientChargeDto>();

            // Define type of notification, convert object
            notification = new Notification {
                Type = NotificationType.Success
            };

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

                    if (id > 0)
                    {
                        dbItem = context.PriceListClientCharge.FirstOrDefault(o => o.PriceListClientChargeID == id);
                    }
                    else
                    {
                        dbItem = new PriceListClientCharge();

                        context.PriceListClientCharge.Add(dbItem);

                        dbItem.CreatedBy   = userId;
                        dbItem.CreatedDate = DateTime.Now;
                    }

                    if (dbItem == null)
                    {
                        notification.Type    = NotificationType.Error;
                        notification.Message = string.Format("Price List Client Charge [id = {0}] not found!", id);

                        return(false);
                    }

                    // Call method convert object to database
                    converter.DTO2DB_PriceListClientCharge(dtoConvert, dbItem);

                    dbItem.UpdatedBy   = userId;
                    dbItem.UpdatedDate = DateTime.Now;

                    // remove orphan detail
                    context.PriceListClientChargeDetail.Local.Where(o => o.PriceListClientCharge == null).ToList()
                    .ForEach(o => context.PriceListClientChargeDetail.Remove(o));

                    context.SaveChanges();

                    // Load again data
                    dtoItem = GetData(dbItem.PriceListClientChargeID, out notification);

                    return(true);
                }
            }
            catch (Exception ex)
            {
                notification.Type    = NotificationType.Error;
                notification.Message = ex.Message;

                return(false);
            }
        }