Пример #1
0
        public IActionResult Edit(int id,
                                  EditGroupViewModel model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var domain = Service <GroupDomain>();
                    var group  = domain.Groups.Id(id);
                    if (group == null)
                    {
                        return(NotFound(new ApiResult()
                        {
                            Code = ResultCode.NotFound,
                            Message = ResultCode.NotFound.DisplayName()
                        }));
                    }

                    domain.EditGroup(group, model);
                    var ev = _eDomain.EditGroup(group, User);
                    _uow.SaveChanges();

                    var data = new Dictionary <string, string>();
                    data["title"]   = "Your group is edited";
                    data["message"] = ev.Message;
                    _eDomain.Notify(new Message
                    {
                        Topic = $"GROUP_MEMBER_{group.Id}",
                        Data  = data
                    });

                    _logger.CustomProperties(new { id, model }).Info("Edit group");

                    return(NoContent());
                }

                var message = "";
                if (ModelState.ContainsKey("name"))
                {
                    message += string.Join('\n',
                                           ModelState["name"].Errors.Select(e => e.ErrorMessage).ToList());
                }

                return(BadRequest(new ApiResult()
                {
                    Code = ResultCode.FailValidation,
                    Data = ModelState,
                    Message = message
                }));
            }
            catch (Exception e)
            {
                _logger.Error(e);
                return(Error(new ApiResult()
                {
                    Code = ResultCode.UnknownError,
                    Message = ResultCode.UnknownError.DisplayName() + ": " + e.Message
                }));
            }
        }
Пример #2
0
        public IActionResult Edit(int id,
                                  EditTaskViewModel model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var domain = Service <TaskDomain>();
                    var task   = domain.Tasks.Id(id);
                    if (task == null)
                    {
                        return(NotFound(new ApiResult()
                        {
                            Code = ResultCode.NotFound,
                            Message = ResultCode.NotFound.DisplayName()
                        }));
                    }

                    domain.EditTask(task, model);
                    var ev = _eDomain.EditTask(task, User);
                    _uow.SaveChanges();

                    if (task.OfUser != task.CreatedUser)
                    {
                        var data = new Dictionary <string, string>();
                        data["title"]   = "Your task has been edited";
                        data["message"] = ev.Message;
                        _eDomain.Notify(new Message
                        {
                            Topic = User.Identity.Name == task.OfUser ? task.CreatedUser : task.OfUser,
                            Data  = data
                        });
                    }

                    _logger.CustomProperties(new { id, model }).Info("Edit task");

                    return(NoContent());
                }

                var message = "";
                if (ModelState.ContainsKey("name"))
                {
                    message += string.Join('\n',
                                           ModelState["name"].Errors.Select(e => e.ErrorMessage).ToList());
                }

                return(BadRequest(new ApiResult()
                {
                    Code = ResultCode.FailValidation,
                    Data = ModelState,
                    Message = message
                }));
            }
            catch (Exception e)
            {
                _logger.Error(e);
                return(Error(new ApiResult()
                {
                    Code = ResultCode.UnknownError,
                    Message = ResultCode.UnknownError.DisplayName() + ": " + e.Message
                }));
            }
        }
        public async Task <IActionResult> Delete(string id)
        {
            try
            {
                var user = await _iDomain.GetUserById(id);

                if (user == null)
                {
                    return(NotFound(new ApiResult()
                    {
                        Code = ResultCode.NotFound,
                        Data = null,
                        Message = ResultCode.NotFound.DisplayName()
                    }));
                }
                var mess  = "";
                var roles = await _iDomain.GetRoles(user);

                if (roles.Contains("Admin"))
                {
                    mess += "Can not remove admin";
                }
                if (!string.IsNullOrEmpty(mess))
                {
                    return(BadRequest(new ApiResult()
                    {
                        Code = ResultCode.FailValidation,
                        Message = mess,
                        Data = null
                    }));
                }

                var ev     = _eDomain.DeleteUser(user, User);
                var tokens = _iDomain.GetTokensNameStartsWith(user, "Firebase", "FCMToken");
                var result = await _iDomain.Remove(user);

                if (result.Succeeded)
                {
                    _uow.SaveChanges();

                    var data = new Dictionary <string, string>();
                    data["title"]   = "So sorry";
                    data["message"] = ev.Message;
                    _eDomain.Notify(new Message
                    {
                        Topic = user.Id,
                        Data  = data
                    });
                    _eDomain.UnsubscribeFromTopic(tokens, user.Id);

                    return(NoContent());
                }
                foreach (var err in result.Errors)
                {
                    ModelState.AddModelError(err.Code, err.Description);
                }
                return(BadRequest(new ApiResult()
                {
                    Code = ResultCode.FailValidation,
                    Data = ModelState,
                    Message = ResultCode.FailValidation.DisplayName()
                }));
            }
            catch (Exception e)
            {
                if (e.InnerException != null &&
                    e.InnerException.GetType() == typeof(SqlException))
                {
                    return(BadRequest(new ApiResult()
                    {
                        Code = ResultCode.FailValidation,
                        Data = null,
                        Message = "Can not delete because it has dependencies"
                    }));
                }
                _logger.Error(e);
                return(Error(new ApiResult()
                {
                    Code = ResultCode.UnknownError,
                    Message = ResultCode.UnknownError.DisplayName() + ": " + e.Message
                }));
            }
        }