Example #1
0
        public override bool DeleteData(int id, out Library.DTO.Notification notification)
        {
            notification = new Library.DTO.Notification()
            {
                Type = Library.DTO.NotificationType.Success
            };
            try
            {
                using (MaterialColorMngEntities context = CreateContext())
                {
                    MaterialColor dbItem = context.MaterialColor.FirstOrDefault(o => o.MaterialColorID == id);
                    if (dbItem == null)
                    {
                        notification.Message = "MaterialColor not found!";
                        return(false);
                    }
                    else
                    {
                        context.MaterialColor.Remove(dbItem);
                        context.SaveChanges();

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

                return(false);
            }
        }
 public void DTO2BD_MaterialColor(DTO.MaterialColorMng.MaterialColor dtoItem, ref MaterialColor dbItem)
 {
     AutoMapper.Mapper.Map <DTO.MaterialColorMng.MaterialColor, MaterialColor>(dtoItem, dbItem);
 }
Example #3
0
        public override bool UpdateData(int id, ref DTO.MaterialColorMng.MaterialColor dtoItem, out Library.DTO.Notification notification)
        {
            notification      = new Notification();
            notification.Type = NotificationType.Success;

            int    number;
            string indexName;

            try
            {
                using (var context = CreateContext())
                {
                    MaterialColor materialColor = null;

                    if (id == 0)
                    {
                        materialColor = new MaterialColor();

                        context.MaterialColor.Add(materialColor);
                    }
                    else
                    {
                        materialColor = context.MaterialColor.FirstOrDefault(o => o.MaterialColorID == id);
                    }

                    if (materialColor == null)
                    {
                        notification.Message = "Material Color not found!";

                        return(false);
                    }
                    else
                    {
                        converter.DTO2BD_MaterialColor(dtoItem, ref materialColor);

                        if (id <= 0)
                        {
                            // Generate code.
                            using (var trans = context.Database.BeginTransaction())
                            {
                                context.Database.ExecuteSqlCommand("SELECT * FROM MaterialColor WITH (TABLOCKX, HOLDLOCK)");

                                try
                                {
                                    var newCode = context.MaterialColorMng_function_GenerateCode().FirstOrDefault();

                                    if (!"***".Equals(newCode))
                                    {
                                        materialColor.MaterialColorUD = newCode;

                                        context.SaveChanges();
                                    }
                                    else
                                    {
                                        notification.Type    = NotificationType.Error;
                                        notification.Message = "Auto generated code exceed maximum option: [ZZZ]";
                                    }
                                }
                                catch (Exception ex)
                                {
                                    trans.Rollback();
                                    throw ex;
                                }
                                finally
                                {
                                    trans.Commit();
                                }
                            }
                        }
                        else
                        {
                            context.SaveChanges();
                        }

                        dtoItem = GetData(materialColor.MaterialColorID, out notification).Data;

                        return(true);
                    }
                }
            }
            catch (DataException exData)
            {
                notification.Type = NotificationType.Error;
                ErrorHelper.DataExceptionParser(exData, out number, out indexName);

                if (number == 2601 && !string.IsNullOrEmpty(indexName))
                {
                    if ("MaterialColorUDUnique".Equals(indexName))
                    {
                        notification.Message = "The Material Color Code is already exists.";
                    }
                }
                else
                {
                    notification.Message = exData.Message;
                }

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

                return(false);
            }
        }