コード例 #1
0
        public async Task <IActionResult> AddAdmin([FromBody] AdminDTO addAdminDTO)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(BadRequest());
                }

                ResponseDTO check = await adminsCrudService.CheckAdminExistForAdd(addAdminDTO.Login);

                if (check.Status == false)
                {
                    return(StatusCode(400, check));
                }

                check = await moderatorsCrudService.CheckModeratorExistForAdd(addAdminDTO.Login);

                if (check.Status == false)
                {
                    return(StatusCode(400, check));
                }

                ResponseDTO answer = await adminsCrudService.AddAdmin(addAdminDTO);

                logger.LogInformation($"Админ: {addAdminDTO.Login}, был добавлен");
                return(Ok(new { answer }));
            }
            catch (ObjectNotFoundException ex)
            {
                logger.LogError(ex.Message);
                return(StatusCode(404, new ResponseDTO()
                {
                    Message = "Данный пользователь не найден", Status = false
                }));
            }
            catch (Exception ex)
            {
                logger.LogError(ex.Message);
                return(StatusCode(500, new ResponseDTO()
                {
                    Status = false
                }));
            }
        }
コード例 #2
0
        public async Task <IActionResult> AddModerator([FromBody] ModeratorDTO addModeratorDTO)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(BadRequest());
                }

                ResponseDTO check = await moderatorsCrudService.CheckModeratorExistForAdd(addModeratorDTO.Login);

                if (check.Status == false)
                {
                    return(Conflict(check));
                }

                check = await adminsCrudService.CheckAdminExistForAdd(addModeratorDTO.Login);

                if (check.Status == false)
                {
                    return(Conflict(check));
                }

                Admin admin = await adminsCrudService.CheckAdminExist(addModeratorDTO.AdminId);

                ResponseDTO answer = await moderatorsCrudService.AddModerator(admin, addModeratorDTO);

                logger.LogInformation($"Модератор: {addModeratorDTO.Login}, был добавлен Админом {admin.Login}");

                Moderator moderator = await moderatorsCrudService.CheckModeratorExist(answer.ResponseData.Id);

                moderator = moderator.ModeratorWithoutPassword();
                return(Ok(new { answer, moderator }));
            }
            catch (Exception ex)
            {
                logger.LogError(ex.Message);
                return(StatusCode(500, new ResponseDTO()
                {
                    Status = false
                }));
            }
        }