예제 #1
0
        public async Task <IActionResult> AddOrUpdate(DepartmentUpdateDTO dto)
        {
            try
            {
                var department = await _departmentRepository.Find(d => d.Name == dto.Name).FirstOrDefaultAsync();

                if (department == null)
                {
                    department = new Department()
                    {
                        Name = dto.Name
                    };
                }

                var users = await _userRepository.Find(u => dto.Ids.Contains(u.ID)).ToListAsync();

                department.Users = users;
                await _departmentRepository.Update(department);

                return(Ok());
            }
            catch (System.Exception ex)
            {
                return(Problem(ex.Message));
            }
        }
예제 #2
0
        public async Task <IActionResult> Update(int id, [FromBody] DepartmentUpdateDTO departmentDTO)
        {
            try
            {
                _logger.LogInfo($"Department Update attempted - id: {id}");
                if (string.IsNullOrWhiteSpace(id.ToString()) || departmentDTO == null ||
                    id != departmentDTO.Id)
                {
                    _logger.LogWarn($"Empty Request was submitted.");
                    return(BadRequest());
                }

                var isExists = await _departmentRepository.IsExists(id);

                if (!isExists)
                {
                    _logger.LogWarn($"Department Update failed: no Department with id: {id} was found.");
                    return(NotFound());
                }

                if (!ModelState.IsValid)
                {
                    _logger.LogWarn($"Department Data was Invalid.");
                    return(BadRequest(ModelState));
                }
                var department = _mapper.Map <Department>(departmentDTO);
                var isSuccess  = await _departmentRepository.Update(department);

                if (!isSuccess)
                {
                    return(InternalError($"Update Operation failed."));
                }

                return(NoContent());
            }
            catch (Exception e)
            {
                return(InternalError($"{e.Message} - {e.InnerException}"));
            }
        }