Example #1
0
 bool checkPermis(T_ProductGroup obj, int actionUser, bool isOwner)
 {
     if (isOwner)
     {
         return(true);
     }
     return(obj.CreatedUser == actionUser);
 }
Example #2
0
        private bool CheckExists(string code, int?id, int?companyId)
        {
            try
            {
                T_ProductGroup obj = null;
                obj = db.T_ProductGroup.FirstOrDefault(x => !x.IsDeleted && x.CompanyId == companyId && x.Name.Trim().ToUpper().Equals(code) && x.Id != id);

                if (obj == null)
                {
                    return(false);
                }
                return(true);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Example #3
0
        public ResponseBase InsertOrUpdate(ProductGroupModel model, bool isOwner)
        {
            try
            {
                using (db = new IEDEntities())
                {
                    var result = new ResponseBase();
                    if (CheckExists(model.Name.Trim().ToUpper(), model.Id, model.CompanyId))
                    {
                        result.IsSuccess = false;
                        result.Errors.Add(new Error()
                        {
                            MemberName = "Insert", Message = "Tên nhóm mã hàng này đã tồn tại. Vui lòng chọn lại Tên khác !."
                        });
                        return(result);
                    }
                    else
                    {
                        T_ProductGroup obj;
                        if (model.Id == 0)
                        {
                            obj = new T_ProductGroup();
                            Parse.CopyObject(model, ref obj);
                            obj.CreatedDate = DateTime.Now;
                            obj.CreatedUser = model.ActionUser;
                            db.T_ProductGroup.Add(obj);
                            db.SaveChanges();
                            result.IsSuccess = true;
                        }
                        else
                        {
                            obj = db.T_ProductGroup.FirstOrDefault(x => !x.IsDeleted && x.Id == model.Id);
                            if (obj == null)
                            {
                                result.IsSuccess = false;
                                result.Errors.Add(new Error()
                                {
                                    MemberName = "Update Product Type", Message = "Nhóm mã hàng bạn đang thao tác đã bị xóa hoặc không tồn tại. Vui lòng kiểm tra lại !."
                                });
                                return(result);
                            }
                            else
                            {
                                if (!checkPermis(obj, model.ActionUser, isOwner))
                                {
                                    result.IsSuccess = false;
                                    result.Errors.Add(new Error()
                                    {
                                        MemberName = "update", Message = "Bạn không phải là người tạo nhóm mã hàng này nên bạn không cập nhật được thông tin cho mã hàng này."
                                    });
                                }
                                else
                                {
                                    obj.CompanyId   = model.CompanyId;
                                    obj.Name        = model.Name;
                                    obj.Description = model.Description;
                                    obj.UpdatedUser = model.ActionUser;
                                    obj.UpdatedDate = DateTime.Now;

                                    db.SaveChanges();
                                    result.IsSuccess = true;
                                }
                            }
                        }

                        return(result);
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }