Beispiel #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 (SubMaterialColorMngEntities context = CreateContext())
                {
                    SubMaterialColor dbItem = context.SubMaterialColor.FirstOrDefault(o => o.SubMaterialColorID == id);
                    if (dbItem == null)
                    {
                        notification.Message = "Sub material not found!";
                        return(false);
                    }
                    else
                    {
                        var item = context.SubMaterialColorMng_SubMaterialColorCheck_View.Where(o => o.SubMaterialColorID == id).FirstOrDefault();
                        //CheckPermission
                        if (item.isUsed.Value == true)
                        {
                            throw new Exception("You can't delete because it used in item other!");
                        }
                        context.SubMaterialColor.Remove(dbItem);
                        context.SaveChanges();

                        return(true);
                    }
                }
            }
            catch (Exception ex)
            {
                notification.Type    = Library.DTO.NotificationType.Error;
                notification.Message = ex.Message;
                return(false);
            }
        }
Beispiel #2
0
        public bool UpdateDataSub(int id, ref DTO.SubMaterialColorMng.SubMaterialColor dtoItem, int iRequesterID, out Library.DTO.Notification notification)
        {
            notification      = new Notification();
            notification.Type = NotificationType.Success;

            int    number;
            string indexName;

            try
            {
                using (var context = CreateContext())
                {
                    SubMaterialColor subMaterialColor = null;

                    if (id == 0)
                    {
                        subMaterialColor = new SubMaterialColor();

                        context.SubMaterialColor.Add(subMaterialColor);
                        subMaterialColor.UpdatedBy   = iRequesterID;
                        subMaterialColor.UpdatedDate = DateTime.Now;
                    }
                    else
                    {
                        var item = context.SubMaterialColorMng_SubMaterialColorCheck_View.Where(o => o.SubMaterialColorID == id).FirstOrDefault();
                        //CheckPermission
                        if (item.isUsed.Value == true)
                        {
                            throw new Exception("You can't update because it used in item other!");
                        }
                        subMaterialColor             = context.SubMaterialColor.FirstOrDefault(o => o.SubMaterialColorID == id);
                        subMaterialColor.UpdatedBy   = iRequesterID;
                        subMaterialColor.UpdatedDate = DateTime.Now;
                    }

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

                        return(false);
                    }
                    else
                    {
                        converter.DTO2BD(dtoItem, ref subMaterialColor);

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

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

                                    if (!"**".Equals(newCode))
                                    {
                                        subMaterialColor.SubMaterialColorUD = newCode;

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

                        dtoItem = GetData(subMaterialColor.SubMaterialColorID, 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 ("SubMaterialColorUDUnique".Equals(indexName))
                    {
                        notification.Message = "The Sub 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);
            }
        }
Beispiel #3
0
 public void DTO2BD(DTO.SubMaterialColorMng.SubMaterialColor dtoItem, ref SubMaterialColor dbItem)
 {
     AutoMapper.Mapper.Map <DTO.SubMaterialColorMng.SubMaterialColor, SubMaterialColor>(dtoItem, dbItem);
 }