Esempio n. 1
0
        public override bool DeleteData(int id, out Notification notification)
        {
            notification = new Notification {
                Type = NotificationType.Success
            };

            try
            {
                using (TransportCIChargeEntities context = CreateContext())
                {
                    TransportCICharge dbItem = context.TransportCICharge.FirstOrDefault(o => o.TransportCIChargeID == id);

                    if (dbItem == null)
                    {
                        return(true);
                    }

                    context.TransportCICharge.Remove(dbItem);
                    context.SaveChanges();
                }

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

                return(false);
            }
        }
Esempio n. 2
0
        public override bool UpdateData(int userId, int id, ref object dtoItem, out Notification notification)
        {
            DTO.TransportCIChargeDto dtoEditItem = ((JObject)dtoItem).ToObject <DTO.TransportCIChargeDto>();
            notification = new Notification {
                Type = NotificationType.Success
            };

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

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

                        context.TransportCICharge.Add(dbItem);

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

                    if (dbItem == null)
                    {
                        notification.Message = string.Format("TransportCostForwarder [id={0}] not found!", id);
                        return(false);
                    }

                    converter.DTO2DB_TransportCICharge(dtoEditItem, ref dbItem);

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

                    // remove orphan items
                    context.TransportCIChargeDetail.Local.Where(o => o.TransportCICharge == null).ToList()
                    .ForEach(o => context.TransportCIChargeDetail.Remove(o));

                    context.SaveChanges();

                    dtoItem = GetData(dbItem.TransportCIChargeID, out notification).Data;

                    return(true);
                }
            }
            catch (Exception ex)
            {
                notification.Type    = NotificationType.Error;
                notification.Message = ex.Message;
                return(false);
            }
        }
Esempio n. 3
0
        public void DTO2DB_TransportCICharge(DTO.TransportCIChargeDto dtoItem, ref TransportCICharge dbItem)
        {
            if (dtoItem.TransportCIChargeDetail != null)
            {
                foreach (var item in dbItem.TransportCIChargeDetail.ToArray())
                {
                    if (!dtoItem.TransportCIChargeDetail.Select(s => s.TransportCIChargeDetailID).Contains(item.TransportCIChargeDetailID))
                    {
                        dbItem.TransportCIChargeDetail.Remove(item);
                    }
                }

                foreach (var dto in dtoItem.TransportCIChargeDetail)
                {
                    TransportCIChargeDetail item;

                    if (dto.TransportCIChargeDetailID < 0)
                    {
                        item = new TransportCIChargeDetail();

                        dbItem.TransportCIChargeDetail.Add(item);
                    }
                    else
                    {
                        item = dbItem.TransportCIChargeDetail.FirstOrDefault(o => o.TransportCIChargeDetailID == dto.TransportCIChargeDetailID);
                    }

                    if (item != null)
                    {
                        Mapper.Map <DTO.TransportCIChargeDetailDto, TransportCIChargeDetail>(dto, item);
                    }
                }
            }

            // Convert string to DateTime Transport Invoice Date
            if (!string.IsNullOrEmpty(dtoItem.InvoiceDate))
            {
                dbItem.InvoiceDate = dtoItem.InvoiceDate.ConvertStringToDateTime();
            }
            else
            {
                dbItem.InvoiceDate = null;
            }

            // Mapping from dto to db
            Mapper.Map <DTO.TransportCIChargeDto, TransportCICharge>(dtoItem, dbItem);
        }