Ejemplo n.º 1
0
        public async Task AddDepartmentAsync(DepartmentCreateDto departmentCreateDto, string email)
        {
            using (unitOfWork)
            {
                var familyOfferingRepository = unitOfWork.FamilyOfferingRepository;
                var departmentRepository     = unitOfWork.DepartmentRepository;

                var familyOffering = await familyOfferingRepository.FindAsync(x =>
                                                                              x.Id == departmentCreateDto.OfferingId &&
                                                                              x.BusinessFamily.CountryBusiness.Country.Organization.User.Email == email
                                                                              );

                if (familyOffering == null)
                {
                    throw new HttpStatusCodeException(404, $"Not found offering with id \"{departmentCreateDto.OfferingId}\" or user with email {email} don't have permisson to manage this organization");
                }

                try
                {
                    departmentRepository.Add(new Department
                    {
                        Name           = departmentCreateDto.Name,
                        FamilyOffering = familyOffering
                    });
                    await unitOfWork.SaveAsync();
                }
                catch (DALException ex)
                {
                    throw new HttpStatusCodeException(400, ex.Message);
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// this method is used to add new department in the database
        /// </summary>
        /// <param name="departmentDetails">data of new department</param>
        /// <returns>returns newly added object</returns>
        public async Task <DepartmentUpdateDto> AddDepartmentsAsync(DepartmentCreateDto departmentDetails)
        {
            var getDepartment = await _departmentRepository.GetDepartmentByNameAsync(departmentDetails.Name);

            if (getDepartment == null)
            {
                var newDepartment = new Department
                {
                    Name      = departmentDetails.Name,
                    IsDeleted = false
                };
                _departmentRepository.AddDepartments(newDepartment);
                await _departmentRepository.SaveChangesAsync();

                return(new DepartmentUpdateDto {
                    Name = newDepartment.Name, Id = newDepartment.Id
                });
            }
            else
            {
                if (getDepartment.IsDeleted == true)
                {
                    getDepartment.IsDeleted = false;
                    await _departmentRepository.SaveChangesAsync();

                    return(new DepartmentUpdateDto {
                        Name = getDepartment.Name, Id = getDepartment.Id
                    });
                }
                else
                {
                    return(null);
                }
            }
        }
        public Result <ApplicationResult <DepartmentDto> > Create(DepartmentCreateDto departmentDto)
        {
            var applicationResult = new ApplicationResult <DepartmentDto>();

            _codeBuilder.Build("DE1", "DE")
            .OnSuccess(resultCode => DepartmentEdit.Create(resultCode, departmentDto.Name, departmentDto.Description))
            .OnSuccess(resultDepartment => {
                if (resultDepartment.IsValid)
                {
                    _departmentRepository.Create(resultDepartment)
                    .OnSuccess(() =>
                    {
                        var dto = Mapper.Map <DepartmentDto>(resultDepartment);

                        applicationResult.IsValid = true;
                        applicationResult.Entity  = dto;
                    });
                }
                else
                {
                    applicationResult.IsValid = false;
                    foreach (var error in resultDepartment.BrokenRules.Errors)
                    {
                        applicationResult.ErrorMessages.Add(new ErrorMessage {
                            Code = error.ErrorCode, Message = error.ErrorMessage
                        });
                    }
                }
            });

            return(applicationResult.ToResult());
        }
        public async Task <ActionResult> Create([FromBody] DepartmentCreateDto departmentCreateDto)
        {
            var mapperDepartment = _mapper.Map <Department>(departmentCreateDto);
            await _departmentRepository.CreateDepartmentAsync(mapperDepartment);

            return(Ok(mapperDepartment));
        }
        public void Post(DepartmentCreateDto department)
        {
            var departmentDT = new Department();

            departmentDT.Title = department.Title;

            _db.Departments.Add(departmentDT);
            _db.SaveChanges();
        }
        public async Task <ActionResult <DepartmentUpdateDto> > AddDepartmentsAsync([FromBody] DepartmentCreateDto departmentDetails)
        {
            var result = await _departmentService.AddDepartmentsAsync(departmentDetails);

            if (result != null)
            {
                return(Created("", result));
            }
            return(BadRequest("Department already exists."));
        }
Ejemplo n.º 7
0
        public async Task CanCreateAndDelete()
        {
            var newDepartment = new DepartmentCreateDto
            {
                CompanyId = 1, Name = "Test Department"
            };
            var department = await _httpClientHelper.PostAsync <DepartmentCreateDto, DepartmentDto>(_baseUrl + "create", newDepartment).ConfigureAwait(false);

            Assert.Equal(newDepartment.Name, department.Name);
            await _httpClientHelper.DeleteAsync(_baseUrl + $"DeleteDepartmentById{API_VERSION}/{department.DepartmentId}").ConfigureAwait(false);
        }
        public async Task <IActionResult> Create(DepartmentCreateDto departmentDto)
        {
            if (await _departmentService.CheckExists(departmentDto.ID))
            {
                return(BadRequest("Department ID already exists!"));
            }
            //var username = User.FindFirst(ClaimTypes.Name).Value;
            //glueIngredientDto.Updated_By = username;
            if (await _departmentService.Add(departmentDto))
            {
                return(NoContent());
            }

            throw new Exception("Creating the department failed on save");
        }
        public async Task <ActionResult <DepartmentDto> > PostDepartment(DepartmentCreateDto departmentCreateDto)
        {
            if (await DepartmentNameExists(departmentCreateDto.DepartmentName))
            {
                return(StatusCode(StatusCodes.Status400BadRequest,
                                  new Response {
                    Success = false, Message = "A department with that name already exists"
                }));
            }

            var department = _mapper.Map <Department>(departmentCreateDto);
            await _departmentsRepo.CreateDepartmentAsync(department);

            await _departmentsRepo.SaveChangesAsync();

            var departmentReadDto = _mapper.Map <DepartmentDto>(department);

            return(CreatedAtAction("GetDepartment", new { id = departmentReadDto.DepartmentId }, departmentReadDto));
        }
        public async Task <bool> Add(DepartmentCreateDto model)
        {
            try
            {
                var cat = _mapper.Map <Department>(model);
                _repoDepartment.Add(cat);
                await _repoDepartment.SaveAll();

                var langs = model.Langs.Select(x => new DepartmentLang {
                    DepartmentID = cat.DepartmentID, Name = x.Name, LanguageID = x.LanguageID
                }).ToList();
                await _repoDepartment.AddDepartmentLang(langs);

                return(true);
            }
            catch (Exception ex)
            {
                return(false);
            }
        }
Ejemplo n.º 11
0
        public IActionResult CreateDepartment(DepartmentCreateDto departmentCreateDto)
        {
            if (departmentCreateDto == null)
            {
                return(BadRequest(ModelState));
            }

            if (_departmentRepo.isDepartmentExists(departmentCreateDto.Name))
            {
                ModelState.AddModelError("", "Department Exists!");
                return(StatusCode(404, ModelState));
            }

            var departmentObj = _mapper.Map <Department>(departmentCreateDto);

            if (!_departmentRepo.CreateDepartment(departmentObj))
            {
                ModelState.AddModelError("", $"Something went wrong when saving the record {departmentObj.Name}");
                return(StatusCode(500, ModelState));
            }

            return(CreatedAtRoute("GetDepartment", new { departmentId = departmentObj.Id }, departmentObj));
        }
Ejemplo n.º 12
0
        public async Task <IActionResult> CreateAsync([FromBody] DepartmentCreateDto department, ApiVersion version)
        {
            Logger.LogDebug("CreateAsync");
            if (!await _repositoryFactory.CompanyRepository.ExistsAsync(c => c.CompanyId == department.CompanyId).ConfigureAwait(false))
            {
                return(NotFound(new { message = $"The Company with id {department.CompanyId} was not found" }));
            }

            var newDepartment = new Department()
            {
                CompanyId = department.CompanyId,
                Name      = department.Name
            };

            var repoDepartment = await _repositoryFactory.DepartmentRepository.AddDepartmentAsync(newDepartment).ConfigureAwait(false);

            var result = _departmentToDtoConverter.Convert(repoDepartment);

            return(new ObjectResult(result)
            {
                StatusCode = StatusCodes.Status201Created
            });
        }
Ejemplo n.º 13
0
        public async Task <ActionResult <DepartmentDto> > PostDepartments(DepartmentCreateDto departmentDto)
        {
            Department departments = new Department {
                Name        = departmentDto.Name,
                Description = departmentDto.Description,
            };

            try
            {
                _context.Departments.Add(departments);
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateException)
            {
                throw;
            }

            return(CreatedAtAction("GetDepartments", new { id = departments.IdDep }, new DepartmentDto {
                IdDep = departments.IdDep,
                Name = departments.Name,
                Description = departments.Description,
                Projects = new List <ProjectDto>()
            }));
        }
 public Task <bool> Update(DepartmentCreateDto model)
 {
     throw new NotImplementedException();
 }
Ejemplo n.º 15
0
 public IActionResult Post([FromBody] DepartmentCreateDto departmentDto)
 {
     return(_departmentApplication.Create(departmentDto)
            .Return(result => Ok(result), (string[] errors) => StatusCode(500, ErrorMessageHelper.BuildSystemErrorMessage(errors))));
 }