public ActionResult Add(DepartmentAddViewModel model)
 {
     _departmentService.Create(new Department
     {
         DepartmentName = model.DepartmentName
     });
     return(RedirectToAction("Index"));
 }
Beispiel #2
0
        public void Add(DepartmentAddViewModel model)
        {
            Depatment depatment = new Depatment
            {
                Name        = model.Name,
                Image       = model.Image,
                Description = model.Description
            };

            _context.Depatments.Add(depatment);
            _context.SaveChanges();
        }
Beispiel #3
0
        public async Task <ActionResult <BaseResponse> > Add(DepartmentAddViewModel req)
        {
            string user = User.Identity.Name;

            if (string.IsNullOrWhiteSpace(user))
            {
                return(Unauthorized("用户凭证缺失"));
            }
            UserMessage um = JsonConvert.DeserializeObject <UserMessage>(user);

            //验证用户权限
            if (!(um.IsAdmin && (um.GroupId == req.GroupId || um.Code == _config["Group"])))
            {
                return(Unauthorized("没有权限"));
            }
            var ret = await _ds.AddDepartmentAsync(req, um.Account);

            return(ret);
        }
Beispiel #4
0
        /// <summary>
        /// 添加部门,已验证是否重名,调用端只需验证权限
        /// </summary>
        /// <param name="req">部门信息</param>
        /// <param name="groupId">组织编号</param>
        /// <returns>返回添加部门是否成功</returns>
        public async Task <BaseResponse> AddDepartmentAsync(DepartmentAddViewModel req, string account)
        {
            int          level = 1;
            string       pathId = null, pathName = null;
            BaseResponse rm = new BaseResponse();

            //查看是否存在多个顶级部门
            if (req.ParentId == null || req.ParentId.Value == 0)
            {
                if (req.DepartmentType == 1)
                {
                    return(new BaseResponse {
                        Success = false, Message = "不能直接添加岗位"
                    });
                }
                var dep = await _department.FindAsync(a => a.GroupId == req.GroupId && (a.ParentId.Value == 0 || a.ParentId == null));

                if (dep != null)
                {
                    return(new BaseResponse {
                        Success = false, Message = "一个组织只能添加一个顶级部门"
                    });
                }
            }
            else
            {
                var parent = await _department.FindAsync(req.ParentId.Value);

                if (parent == null)
                {
                    return(new BaseResponse {
                        Success = false, Message = "输入的父部门不存在"
                    });
                }
                level = parent.Level + 1;
                if (parent.ParentId == null)
                {
                    pathId   = parent.Id.ToString();
                    pathName = parent.DepartmentName;
                }
                else
                {
                    pathId   = $"{ parent.PathId}/{parent.Id}";
                    pathName = $"{parent.PathName}/{parent.DepartmentName}";
                }
                if (parent.DepartmentType == DepartmentType.Station)
                {
                    return(new BaseResponse {
                        Success = false, Message = "岗位下不能再添加部门或者岗位"
                    });
                }
            }

            //同一部门下的子部门不能重名
            var d = _department.Find(a => a.ParentId == req.ParentId && a.DepartmentName == req.Name && a.GroupId == req.GroupId).FirstOrDefault();

            if (d != null)
            {
                rm.Success = false;
                rm.Message = "该组织下存在相同的部门名称";
                return(rm);
            }
            try
            {
                var data = _map.Map <DepartmentModel>(req);
                data.Create   = account;
                data.GroupId  = req.GroupId;
                data.Level    = level;
                data.PathId   = pathId;
                data.PathName = pathName;
                await _department.AddAsync(data);

                rm = new BResponse <int>()
                {
                    Success = true,
                    Message = "添加数据成功",
                    Data    = data.Id
                };
                _log.LogInformation($"{account}添加id为{data.Id},名称为:{req.Name}的部门成功");
            }
            catch (Exception ex)
            {
                rm.Success = false;
                rm.Message = "添加数据失败";
                _log.LogError($"{account}添加名称为{req.Name}的部门失败,失败原因:{ex.Message}->{ex.StackTrace}->{ex.InnerException}");
            }
            return(rm);
        }
 public ActionResult Add(DepartmentAddViewModel model)
 {
     _departmentService.Add(model);
     return(RedirectToAction("Index"));
 }
        public async Task <PartialViewResult> NewDepartment(DepartmentAddViewModel model)
        {
            if (ModelState.IsValid)
            {
                var stringBuilder = new StringBuilder();

                try
                {
                    // If model state is valid, attempt to persist new department
                    var department = Mapper.Map <DepartmentAddViewModel, Department>(model);

                    var response = await this.GetHttpClient().PostAsJsonAsync("Department", department);

                    if (response.IsSuccessStatusCode)
                    {
                        department = await response.Content.ReadAsAsync <Department>();

                        if (department != null)
                        {
                            var notification = Mapper.Map <NotificationAddViewModel, Notification>(new NotificationAddViewModel("New Department Confirmation", String.Format("Department '{0}' ({1}) has been successfully created.", department.Name, department.DepartmentId.ToString("DEP00000"))));

                            response = await this.GetHttpClient().PostAsJsonAsync(String.Format("Notification?adminUsername={0}", User.Identity.Name), notification); // Attempt to persist notification to the data context

                            if (!response.IsSuccessStatusCode)
                            {
                                throw new NotificationAddException(String.Format("Notification could NOT be added. Status Code: {1}", response.StatusCode));
                            }

                            var departmentAddEditConfirmationViewModel = Mapper.Map <Department, DepartmentAddEditConfirmationViewModel>(department);

                            return(PartialView("_NewDepartmentConfirmation", departmentAddEditConfirmationViewModel));
                        }
                        else
                        {
                            throw new DepartmentAddException("Null is returned after creating new department. Status Code: " + response.StatusCode);
                        }
                    }
                    else
                    {
                        // If department could not be created, throw DepartmentAddException exception
                        throw new DepartmentAddException("Department " + department.Name + " could not be created. Response: " + response.StatusCode);
                    }
                }
                catch (DepartmentAddException ex)
                {
                    // Log exception
                    ErrorHandlingUtilities.LogException(ErrorHandlingUtilities.GetExceptionDetails(ex));

                    stringBuilder.Append("<div class='text-center'><h4><strong>Department could NOT be created.</strong></h4></div>");

                    stringBuilder.Append("<div class='row'><div class='col-md-12'><p></p></div><div class='col-md-offset-1 col-md-11'>An exception has been caught while attempting to update an existing department. Please review an exception log for more details about the exception.</div></div>");

                    var confirmationViewModel = new ConfirmationViewModel(stringBuilder.ToString());

                    return(PartialView("_Error", confirmationViewModel));
                }
                catch (NotificationAddException ex)
                {
                    // Log exception
                    ErrorHandlingUtilities.LogException(ErrorHandlingUtilities.GetExceptionDetails(ex));

                    stringBuilder.Append("<div class='text-center'><h4><strong>Failed to create new notification.</strong></h4></div>");

                    stringBuilder.Append(String.Format("<div class='row'><div class='col-md-12'><p></p></div><div class='col-md-offset-1 col-md-11'>An exception has been thrown while attempting to create new notification.</div>"));

                    stringBuilder.Append("<div class='col-md-12'><p></p></div><div class='col-md-offset-1 col-md-11'><strong>NOTE:</strong> Please review an exception log for more information about the exception.</div></div>");

                    var confirmationViewModel = new ConfirmationViewModel(stringBuilder.ToString());

                    return(PartialView("_Error", confirmationViewModel));
                }
            }

            return(PartialView("_NewDepartment", model));
        }
        public PartialViewResult NewDepartment()
        {
            var departmentAddViewModel = new DepartmentAddViewModel();

            return(PartialView("_NewDepartment", departmentAddViewModel));
        }