Exemplo n.º 1
0
        public void DTO2BD(BackCushionDTO dtoItem, ref BackCushion dbItem)
        {
            AutoMapper.Mapper.Map <BackCushionDTO, BackCushion>(dtoItem, dbItem);

            // map child
            if (dtoItem.ProductGroups != null)
            {
                // map child rows
                foreach (ProductGroupDTO dtoGroup in dtoItem.ProductGroups)
                {
                    BackCushionProductGroup dbGroup;
                    if (dtoGroup.BackCushionProductGroupID <= 0)
                    {
                        dbGroup = new BackCushionProductGroup();
                        dbItem.BackCushionProductGroup.Add(dbGroup);
                        dbItem.CreatedBy   = dtoItem.CreatedBy;
                        dbItem.CreatedDate = DateTime.Now;
                    }
                    else
                    {
                        dbGroup            = dbItem.BackCushionProductGroup.FirstOrDefault(o => o.BackCushionProductGroupID == dtoGroup.BackCushionProductGroupID);
                        dbItem.UpdatedBy   = dtoItem.UpdatedBy;
                        dbItem.UpdatedDate = DateTime.Now;
                    }

                    if (dbGroup != null)
                    {
                        AutoMapper.Mapper.Map <ProductGroupDTO, BackCushionProductGroup>(dtoGroup, dbGroup);
                    }
                }
            }
        }
Exemplo n.º 2
0
        public override bool UpdateData(int userId, int id, ref object dtoItem, out Notification notification)
        {
            BackCushionDTO dtoPBackCushion = ((Newtonsoft.Json.Linq.JObject)dtoItem).ToObject <BackCushionDTO>();

            notification = new Library.DTO.Notification()
            {
                Type = Library.DTO.NotificationType.Success
            };
            try
            {
                using (BackCushionMngEntities context = CreateContext())
                {
                    BackCushion dbItem = null;
                    if (id == 0)
                    {
                        dbItem = new BackCushion();
                        context.BackCushion.Add(dbItem);
                        dtoPBackCushion.CreatedBy = userId;
                    }
                    else
                    {
                        var item = context.BackCushionMng_BackCushionCheck_View.Where(o => o.BackCushionID == id).FirstOrDefault();
                        //CheckPermission
                        if (item.isUsed.Value == true)
                        {
                            throw new Exception("You can't update because it used in item other!");
                        }
                        dbItem = context.BackCushion.FirstOrDefault(o => o.BackCushionID == id);
                        dtoPBackCushion.UpdatedBy = userId;
                    }

                    if (dbItem == null)
                    {
                        notification.Message = "Back cushion not found!";
                        return(false);
                    }
                    else
                    {
                        // check concurrency
                        if (dbItem.ConcurrencyFlag != null && !dbItem.ConcurrencyFlag.SequenceEqual(Convert.FromBase64String(dtoPBackCushion.ConcurrencyFlag_String)))
                        {
                            throw new Exception(DALBase.Helper.TEXT_CONCURRENCY_CONFLICT);
                        }
                        converter.DTO2BD(dtoPBackCushion, ref dbItem);
                        // processing image
                        if (dtoPBackCushion.ImageFile_HasChange)
                        {
                            dbItem.ImageFile = fwFactory.CreateFilePointer(this._TempFolder, dtoPBackCushion.ImageFile_NewFile, dtoPBackCushion.ImageFile);
                        }

                        if (id <= 0)
                        {
                            // generate code
                            using (DbContextTransaction scope = context.Database.BeginTransaction())
                            {
                                context.Database.ExecuteSqlCommand("SELECT * FROM BackCushion WITH (TABLOCKX, HOLDLOCK)");
                                try
                                {
                                    var newCode = context.BackCushionMng_function_GenerateCode().FirstOrDefault();
                                    if (newCode != "*")
                                    {
                                        dbItem.BackCushionUD = newCode;
                                    }
                                    else
                                    {
                                        throw new Exception("Auto generated code exceed maximum option: [Z]");
                                    }
                                    context.SaveChanges();
                                }
                                catch (Exception ex)
                                {
                                    throw ex;
                                }
                                finally
                                {
                                    scope.Commit();
                                }
                            }
                        }
                        else
                        {
                            context.SaveChanges();
                        }

                        // Handle notification missing information.
                        string emailSubject = (id == 0) ? "TASK REQUEST [CREATE BACK CUSHION]" : "TASK REQUEST [UPDATE BACK CUSHION]";
                        string emailBody    = string.Empty;

                        if (!IsNullPropertiesBackCushion(dbItem, ref emailBody))
                        {
                            SendToEmailNotification(context, emailSubject, emailBody);
                        }

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

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

                return(false);
            }
        }