Ejemplo n.º 1
0
        public async Task <IActionResult> AddNewDepartment([FromBody] CreateDepartmentRequestModel departmentCreateRequestModel)
        {
            try
            {
                System.Threading.Thread.Sleep(1000);
                var departmentExists = await _departmentRepository.DepartmentExists(departmentCreateRequestModel.DepartmentName);

                if (departmentExists)
                {
                    return(BadRequest(new ApiResponseBadRequestResult()
                    {
                        ErrorMessage = $"Department {departmentCreateRequestModel.DepartmentName} already exists"
                    }));
                }

                var newDepartment = await _departmentRepository.AddDepartment(_mapper.Map <Department>(departmentCreateRequestModel));

                return(Ok(new ApiResponseOKResult()
                {
                    Data = newDepartment
                }));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Failed to create new department");
                return(StatusCode((int)HttpStatusCode.InternalServerError, new ApiResponseFailure()
                {
                    ErrorMessage = "Failed to create new department"
                }));
            }
        }
Ejemplo n.º 2
0
        public async Task<ActionResult<int>> Create(CreateDepartmentRequestModel model)
        {
            var userId = this.User.GetId();

            var result = await this.departmentServcie.CreateDepartment(model, userId);

            return Created(nameof(this.Create), result);
        }
Ejemplo n.º 3
0
        public async Task <int> CreateDepartment(CreateDepartmentRequestModel model, string userId)
        {
            Garage garage = await this.dbContext.Employees
                            .Where(x => x.UserId == userId && x.Garage.Id == model.GarageId)
                            .Select(x => x.Garage)
                            .FirstAsync();

            var department = new Department()
            {
                Name = model.DepartmentName,
            };

            garage.Departments.Add(department);

            this.dbContext.Update(garage);

            await this.dbContext.SaveChangesAsync();

            return(department.Id);
        }