Пример #1
0
 public void DTO2DB_UpdateData(DTO.FactoryLocation dtoItem, ref Location dbItem)
 {
     AutoMapper.Mapper.Map <DTO.FactoryLocation, Location>(dtoItem, dbItem);
 }
Пример #2
0
        public bool UpdateData(int userId, int id, ref object dtoItem, out Library.DTO.Notification notification)
        {
            notification      = new Library.DTO.Notification();
            notification.Type = Library.DTO.NotificationType.Success;

            DTO.FactoryLocation factoryLocation = ((Newtonsoft.Json.Linq.JObject)dtoItem).ToObject <DTO.FactoryLocation>();
            bool resultUpdate = false;

            // Check location code is null.
            if (string.IsNullOrEmpty(factoryLocation.LocationUD.Trim()))
            {
                notification.Type    = Library.DTO.NotificationType.Error;
                notification.Message = "Please input Location Code!";

                return(resultUpdate);
            }

            try
            {
                using (var context = CreateContext())
                {
                    Location location = null;

                    if (id < 0)
                    {
                        // Check location code exist.
                        location = context.Location.FirstOrDefault(o => o.LocationUD == factoryLocation.LocationUD);
                        if (location != null)
                        {
                            notification.Type    = Library.DTO.NotificationType.Error;
                            notification.Message = "Location already in database!";

                            return(resultUpdate);
                        }
                        else
                        {
                            location = new Location();
                        }

                        context.Location.Add(location);
                    }
                    else
                    {
                        location = context.Location.FirstOrDefault(o => o.LocationID == id);

                        if (location == null)
                        {
                            notification.Type    = Library.DTO.NotificationType.Error;
                            notification.Message = "Cannot found data!";

                            return(resultUpdate);
                        }
                    }

                    converter.DTO2DB_UpdateData(factoryLocation, ref location);

                    if (id < 0)
                    {
                        location.CreatedBy   = userId;
                        location.CreatedDate = DateTime.Now;
                    }

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

                    location.LocationUD = factoryLocation.LocationUD;

                    context.SaveChanges();

                    dtoItem = converter.DB2DTO_FactoryLocation(context.FactoryLocationMng_Location_View.FirstOrDefault(o => o.LocationID == location.LocationID));

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

                if (ex.InnerException != null && !string.IsNullOrEmpty(ex.InnerException.Message))
                {
                    notification.DetailMessage.Add(ex.InnerException.Message);
                }
            }

            return(resultUpdate);
        }