Ejemplo n.º 1
0
        public void DTO2DB_FactoryAssignment(DTO.FactoryAssignment.ProductDTO dtoItem, ref SampleProduct dbItem)
        {
            AutoMapper.Mapper.Map <DTO.FactoryAssignment.ProductDTO, SampleProduct>(dtoItem, dbItem);
            if (!string.IsNullOrEmpty(dtoItem.FactoryDeadline))
            {
                if (DateTime.TryParse(dtoItem.FactoryDeadline, nl, System.Globalization.DateTimeStyles.None, out tmpDate))
                {
                    dbItem.FactoryDeadline = tmpDate;
                }
            }

            // monitor
            foreach (SampleProductSubFactory dbSubFactory in dbItem.SampleProductSubFactory.ToArray())
            {
                if (!dtoItem.SubFactoryDTOs.Select(o => o.SampleProductSubFactoryID).Contains(dbSubFactory.SampleProductSubFactoryID))
                {
                    dbItem.SampleProductSubFactory.Remove(dbSubFactory);
                }
            }
            foreach (DTO.FactoryAssignment.SubFactoryDTO dtoSubFactory in dtoItem.SubFactoryDTOs)
            {
                SampleProductSubFactory dbSubFactory;
                if (dtoSubFactory.SampleProductSubFactoryID <= 0)
                {
                    dbSubFactory = new SampleProductSubFactory();
                    dbItem.SampleProductSubFactory.Add(dbSubFactory);
                }
                else
                {
                    dbSubFactory = dbItem.SampleProductSubFactory.FirstOrDefault(o => o.SampleProductSubFactoryID == dtoSubFactory.SampleProductSubFactoryID);
                }

                if (dbSubFactory != null)
                {
                    AutoMapper.Mapper.Map <DTO.FactoryAssignment.SubFactoryDTO, SampleProductSubFactory>(dtoSubFactory, dbSubFactory);
                }
            }
        }
Ejemplo n.º 2
0
        public bool UpdateFactoryAssignmentData(int userId, int id, ref object dtoItem, out Library.DTO.Notification notification)
        {
            DTO.FactoryAssignment.ProductDTO dtoProduct = ((Newtonsoft.Json.Linq.JObject)dtoItem).ToObject <DTO.FactoryAssignment.ProductDTO>();
            notification = new Library.DTO.Notification()
            {
                Type = Library.DTO.NotificationType.Success
            };
            try
            {
                using (Sample3MngEntities context = CreateContext())
                {
                    SampleProduct dbItem = context.SampleProduct.FirstOrDefault(o => o.SampleProductID == id);
                    if (dbItem == null)
                    {
                        notification.Message = "Sample Product not found!";
                        return(false);
                    }
                    else
                    {
                        converter.DTO2DB_FactoryAssignment(dtoProduct, ref dbItem);
                        context.SaveChanges();
                    }

                    dtoItem = GetFactoryAssignmentData(dbItem.SampleProductID, out notification).Data;
                    return(true);
                }
            }
            catch (Exception ex)
            {
                notification = new Library.DTO.Notification()
                {
                    Message = ex.Message, Type = Library.DTO.NotificationType.Error
                };
                return(false);
            }
        }