Ejemplo n.º 1
0
        private bool CheckExists(string keyword, int id, bool isCheckName, IEDEntities db)
        {
            try
            {
                T_TimeTypePrepare timeType = null;
                keyword = keyword.Trim().ToUpper();
                if (isCheckName)
                {
                    timeType = db.T_TimeTypePrepare.FirstOrDefault(x => !x.IsDeleted && x.Id != id && x.Name.Trim().ToUpper().Equals(keyword));
                }
                else
                {
                    timeType = db.T_TimeTypePrepare.FirstOrDefault(x => !x.IsDeleted && x.Id != id && x.Code.Trim().ToUpper().Equals(keyword));
                }

                if (timeType == null)
                {
                    return(false);
                }
                return(true);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Ejemplo n.º 2
0
        public ResponseBase InsertOrUpdate(TimeTypePrepareModel model, bool isOwner)
        {
            try
            {
                using (db = new IEDEntities())
                {
                    var result = new ResponseBase();
                    bool flag = false;
                    if (CheckExists(model.Name.Trim().ToUpper(), model.Id, true, db))
                    {
                        result.IsSuccess = false;
                        result.Errors.Add(new Error() { MemberName = "Insert  ", Message = "Tên này đã được sử dụng. Vui lòng chọn lại Tên khác !." });
                        flag = true;
                    }
                    if (!string.IsNullOrEmpty(model.Code))
                    {
                        if (CheckExists(model.Code.Trim().ToUpper(), model.Id, false, db))
                        {
                            result.IsSuccess = false;
                            result.Errors.Add(new Error() { MemberName = "Insert  ", Message = "Mã này đã được sử dụng. Vui lòng chọn lại Mã khác !." });
                            flag = true;
                        }
                    }
                    if (!flag)
                    {
                        T_TimeTypePrepare obj = null;
                        if (model.Id == 0)
                        {
                            obj = new T_TimeTypePrepare();
                            Parse.CopyObject(model, ref obj);
                            obj.CreatedDate = DateTime.Now;
                            obj.CreatedUser = model.ActionUser;
                            db.T_TimeTypePrepare.Add(obj);
                            db.SaveChanges();
                            result.IsSuccess = true;
                        }
                        else
                        {
                            obj = db.T_TimeTypePrepare.FirstOrDefault(x => !x.IsDeleted && x.Id == model.Id);
                            if (obj == null)
                            {
                                result.IsSuccess = false;
                                result.Errors.Add(new Error() { MemberName = "Update", Message = "Dữ liệu 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 loại thời gian chuẩn bị này nên bạn không cập nhật được thông tin cho loại thời gian chuẩn bị này." });
                                }
                                else
                                {
                                    obj.Name = model.Name;
                                    obj.Code = model.Code;
                                    obj.IsPublic = model.IsPublic;
                                    obj.Description = model.Description;
                                    obj.UpdatedUser = model.ActionUser;
                                    obj.UpdatedDate = DateTime.Now;
                                    db.SaveChanges();
                                    result.IsSuccess = true;
                                }
                            }
                        }
                        
                    }
                    return result;
                }

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Ejemplo n.º 3
0
 bool checkPermis(T_TimeTypePrepare obj, int actionUser, bool isOwner)
 {
     if (isOwner)
         return true;
     return obj.CreatedUser == actionUser;
 }