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 (SubMaterialMngEntities context = CreateContext())
                {
                    SubMaterial dbItem = context.SubMaterial.FirstOrDefault(o => o.SubMaterialID == id);
                    if (dbItem == null)
                    {
                        notification.Message = "Sub material not found!";
                        return(false);
                    }
                    else
                    {
                        var item = context.SubMaterialMng_SubMaterialCheck_View.Where(o => o.SubMaterialID == id).FirstOrDefault();
                        //CheckPermission
                        if (item.isUsed.Value == true)
                        {
                            throw new Exception("You can't delete because it used in item other!");
                        }
                        context.SubMaterial.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.SubMaterialMng.SubMaterial dtoItem, int iRequesterID, out Library.DTO.Notification notification)
        {
            notification      = new Notification();
            notification.Type = NotificationType.Success;

            int    number;
            string indexName;

            try
            {
                using (var context = CreateContext())
                {
                    SubMaterial subMaterial = null;

                    if (id == 0)
                    {
                        subMaterial = new SubMaterial();

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

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

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

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

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

                                    if (!"**".Equals(newCode))
                                    {
                                        subMaterial.SubMaterialUD = 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(subMaterial.SubMaterialID, 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 ("SubMaterialUDUnique".Equals(indexName))
                    {
                        notification.Message = "The Sub Material Code is already exists.";
                    }
                }
                else
                {
                    notification.Message = exData.Message;
                }

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

                return(false);
            }
        }
 public void DTO2BD(DTO.SubMaterialMng.SubMaterial dtoItem, ref SubMaterial dbItem)
 {
     AutoMapper.Mapper.Map <DTO.SubMaterialMng.SubMaterial, SubMaterial>(dtoItem, dbItem);
 }