public async Task <TimeCardRegisterDto> AddTimeCardRegister(TimeCardRegisterDto timeCardRegisterDto) { var user = _userRepository.GetByEmail(timeCardRegisterDto.UserEmail); if (user == null) { throw new EntityNotFoundException <User>(timeCardRegisterDto.UserId); } if (timeCardRegisterDto.ProjectId == 0) { throw new ArgumentNullException(nameof(timeCardRegisterDto.ProjectId), "O valor do Id do Projeto não pode ser 0"); } var project = _projectRepository.Get(timeCardRegisterDto.ProjectId); if (project == null) { throw new EntityNotFoundException <Project>(timeCardRegisterDto.ProjectId); } var timeCardRegister = new TimeCardRegister() { StartDate = timeCardRegisterDto.StartDate, User = user, Project = project }; timeCardRegister = await _timeCardRepository.AddAsync(timeCardRegister); var model = _mapper.Map <TimeCardRegisterDto>(timeCardRegister); return(model); }
public async Task <IActionResult> EditTimeCardRegister([FromBody] TimeCardRegisterDto timeCardRegister) { if (!User.Identity.IsAuthenticated) { return(new UnauthorizedResult()); } var user = _userService.GetUserByEmail(User.Identity.Name); if (timeCardRegister.UserId != user.Id || !User.IsInRole("DP_ADMIN")) { return(new ForbidResult()); } timeCardRegister = await _timeCardService.EditTimeCardRegister(timeCardRegister); return(new JsonResult(timeCardRegister)); }
public async Task <TimeCardRegisterDto> EditTimeCardRegister(TimeCardRegisterDto timeCardRegisterDto) { var timeCardRegister = GetTimeCardById(timeCardRegisterDto.Id); if (timeCardRegisterDto.StartDate != DateTime.MinValue) { timeCardRegister.StartDate = timeCardRegisterDto.StartDate; } if (timeCardRegisterDto.EndDate != DateTime.MinValue) { timeCardRegister.EndDate = timeCardRegisterDto.EndDate; } if (timeCardRegisterDto.ProjectId == 0) { throw new ArgumentNullException(nameof(timeCardRegisterDto.ProjectId), "O valor do Id do Projeto não pode ser 0"); } if (timeCardRegisterDto.ProjectId != timeCardRegister.Project.Id) { var project = _projectRepository.Get(timeCardRegisterDto.ProjectId); if (project == null) { throw new EntityNotFoundException <Project>(timeCardRegisterDto.ProjectId); } timeCardRegister.Project = project; } await _timeCardRepository.UpdateAsync(timeCardRegister); return(_mapper.Map <TimeCardRegisterDto>(timeCardRegister)); }