Exemple #1
0
 bool checkPermis(SWorkerLevel obj, int actionUser, bool isOwner)
 {
     if (isOwner)
     {
         return(true);
     }
     return(obj.CreatedUser == actionUser);
 }
Exemple #2
0
        public ResponseBase InsertOrUpdate(WorkerLevelModel model, int[] relationCompanyId, bool isOwner)
        {
            var result = new ResponseBase();

            result.IsSuccess = false;
            var flag = false;

            try
            {
                using (db = new IEDEntities())
                {
                    if (CheckExists(model.Name, model.Id, model.CompanyId ?? 0, relationCompanyId, db))
                    {
                        flag             = true;
                        result.IsSuccess = false;
                        result.Errors.Add(new Error()
                        {
                            MemberName = "Create  ", Message = "Tên Bậc Thợ này Đã Tồn Tại, Vui Lòng Chọn Tên Khác"
                        });
                    }

                    if (!flag)
                    {
                        SWorkerLevel obj = null;
                        if (model.Id == 0)
                        {
                            obj = new SWorkerLevel();
                            Parse.CopyObject(model, ref obj);
                            obj.CreatedDate = DateTime.Now;
                            obj.CompanyId   = null;
                            if (model.IsPrivate)
                            {
                                obj.CompanyId = model.CompanyId;
                            }
                            db.SWorkerLevels.Add(obj);
                            db.SaveChanges();;
                            result.IsSuccess = true;
                        }
                        else
                        {
                            obj = db.SWorkerLevels.FirstOrDefault(x => x.Id == model.Id && !x.IsDeleted);
                            if (obj != null)
                            {
                                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 bậc thợ này nên bạn không cập nhật được thông tin cho bậc thợ này."
                                    });
                                }
                                else
                                {
                                    obj.Coefficient = model.Coefficient;
                                    obj.CompanyId   = null;
                                    if (model.IsPrivate)
                                    {
                                        obj.CompanyId = model.CompanyId;
                                    }
                                    obj.Name        = model.Name;
                                    obj.Note        = model.Note;
                                    obj.UpdatedDate = DateTime.Now;
                                    obj.UpdatedUser = model.ActionUser;
                                    db.SaveChanges();;
                                    result.IsSuccess = true;
                                }
                            }
                            else
                            {
                                result.IsSuccess = false;
                                result.Errors.Add(new Error()
                                {
                                    MemberName = "Update ", Message = "Dữ Liệu Bậc Thợ bạn đang thao tác không tồn tại hoặc đã bị xóa.\nVui lòng kiểm tra lại!"
                                });
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return(result);
        }