public async Task <IActionResult> AddDepartment(int userId, DepartmentForCreationDto departmentForCreation)
        {
            var creator = await _userRepo.GetUser(userId);

            Console.WriteLine(int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value));

            if (creator.Id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            var department = _mapper.Map <Department>(departmentForCreation);

            department.User = creator;

            _repo.Add(department);

            if (await _repo.SaveAll())
            {
                var jobToReturn = _mapper.Map <DepartmentForCreationDto>(department);
                return(CreatedAtRoute("GetDepartment", new { deptName = department.DeptName, userId = userId }, jobToReturn));
            }

            throw new Exception("Creation of Department failed on save");
        }
Ejemplo n.º 2
0
        public async Task <DepartmentForReturnDto> Update(DepartmentForCreationDto updateDto)
        {
            var checkById = await departmentDal.GetAsync(x => x.Id == updateDto.Id);

            if (checkById == null)
            {
                throw new RestException(HttpStatusCode.BadRequest, new { NotFound = Messages.NotFound });
            }

            var mapForUpdate = mapper.Map(updateDto, checkById);
            await departmentDal.Update(mapForUpdate);

            return(mapper.Map <Department, DepartmentForReturnDto>(mapForUpdate));
        }
        public async Task <IActionResult> GetDepartment(int userId, string deptName)
        {
            var creator = await _userRepo.GetUser(userId);

            if (creator.Id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            var departmentFromRepo = _repo.GetDepartment(userId, deptName);

            DepartmentForCreationDto departmentForReturn = _mapper.Map <DepartmentForCreationDto>(departmentFromRepo);

            return(Ok(departmentForReturn));
        }
Ejemplo n.º 4
0
        public async Task <DepartmentForReturnDto> Create(DepartmentForCreationDto createDto)
        {
            var checkByName = await departmentDal.GetAsync(x => x.Name.ToLower() == createDto.Name.ToLower());

            if (checkByName != null)
            {
                throw new RestException(HttpStatusCode.BadRequest, new { AlreadyExist = Messages.AlreadyExist });
            }

            var mapForCreate = mapper.Map <Department>(createDto);
            var saveToDb     = await departmentDal.Add(mapForCreate);

            var mapForReturn = mapper.Map <Department, DepartmentForReturnDto>(saveToDb);

            return(mapForReturn);
        }
 public async Task <ActionResult <DepartmentForReturnDto> > Update(DepartmentForCreationDto updateDto)
 {
     return(await departmentService.Update(updateDto));
 }