コード例 #1
0
        public IActionResult TeamUpdate(int id, [FromBody] TeamUpdateDto teamUpdate)
        {
            var teamEntity = _transActionRepo.GetTeam(id);

            if (teamEntity == null)
            {
                return(NotFound());
            }
            if (teamUpdate == null)
            {
                return(NotFound());
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            Mapper.Map(teamUpdate, teamEntity);


            if (!_transActionRepo.Save())
            {
                return(StatusCode(500, "A problem happened while handling your request."));
            }

            return(NoContent());
        }
コード例 #2
0
        public IActionResult UpdateTeam(int id, [FromBody] TeamUpdateDto teamUpdate)
        {
            string userGuid = UserHelper.GetUserGuid(_httpContextAccessor);
            var    getUser  = _unitOfWork.User.GetByGuid(userGuid);

            var user = _unitOfWork.User.GetCurrentUser(getUser.Guid);

            if (user.UserId == teamUpdate.UserId || user.Role.Name.ToLower() == "admin") // checking if the user is the team Leader or an admin.
            {
                if (!ModelState.IsValid)
                {
                    return(BadRequest(new TransActionResponse(ModelState)));
                }
                var teamEntity = _unitOfWork.Team.GetById(id);
                if (teamEntity == null)
                {
                    return(StatusCode(StatusCodes.Status404NotFound, new TransActionResponse("Team Not Found")));
                }

                _mapper.Map(teamUpdate, teamEntity);
                _unitOfWork.Team.Update(teamEntity);
                if (!_unitOfWork.Save())
                {
                    return(StatusCode(500, new TransActionResponse("A problem happened while handling your request.")));
                }

                return(GetTeamById(id));
            }
            else
            {
                return(StatusCode(401, new TransActionResponse("Unauthorized Access")));
            }
        }
コード例 #3
0
ファイル: TeamRepository.cs プロジェクト: SutoZ/FormulaRace
        public async Task UpdateTeamAsync(int id, TeamUpdateDto updateDto)
        {
            var team = await context.Teams.FirstOrDefaultAsync(x => x.Id == id);

            team = updateDto.UpdateModelObject(team);

            await context.SaveChangesAsync();
        }
コード例 #4
0
ファイル: TeamController.cs プロジェクト: Ueionabaru/BitTest
        public async Task <IActionResult> Update([FromRoute] int id, [FromBody] TeamUpdateDto teamUpdateDto)
        {
            var team = await _repository.GetAsync(id);

            _mapper.Map(teamUpdateDto, team);
            _repository.Update(team);
            var isSuccess = await _repository.SaveChangesAsync();

            if (!isSuccess)
            {
                return(BadRequest());
            }
            return(NoContent());
        }
コード例 #5
0
 public TeamReadDto UpdateTeamName(TeamUpdateDto teamUpdateDto)
 {
     try
     {
         var updatedTeam = _unitOfWork.TeamRepository.UpdateTeamName(_mapper.Map <Team>(teamUpdateDto));
         _unitOfWork.Commit();
         return(_mapper.Map <TeamReadDto>(updatedTeam));
     }
     catch (DbUpdateException ex) when((ex.InnerException as SqlException)?.Number == 2601)
     {
         _unitOfWork.Rollback();
         throw new Exception($"Team already exists with the name {teamUpdateDto.Name}", ex);
     }
 }
コード例 #6
0
        public ActionResult <TeamReadDto> UpdateTeamName(TeamUpdateDto teamUpdateDto)
        {
            if (teamUpdateDto == null)
            {
                return(BadRequest());
            }

            try
            {
                return(Ok(_service.UpdateTeamName(teamUpdateDto)));
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }
        }
コード例 #7
0
        public async void TeamService_UpdateTeamAsync_ThrowsNotFoundWhenTeamDoesNotExist()
        {
            // arrange
            Mock <IRepository> mockRepo = new Mock <IRepository>();

            mockRepo.Setup(r => r.GetTeamAsync(It.IsAny <int>())).ReturnsAsync((TeamDto)null);
            TeamService   target = new TeamService(mockRepo.Object);
            int           teamId = 1;
            TeamUpdateDto dto    = new TeamUpdateDto
            {
                Name = "Anne Telohps",
                Zip  = "90210"
            };

            // act & assert
            await Assert.ThrowsAsync <NotFoundException>(async() => await target.UpdateTeamAsync(teamId, dto));
        }
コード例 #8
0
        public async void TeamsController_UpdateTeam_ReturnsOkAndCallsTeamService()
        {
            // Arrange
            Mock <IManageTeams> mockTeamService = new Mock <IManageTeams>();

            mockTeamService.Setup(s => s.UpdateTeamAsync(It.IsAny <int>(), It.IsAny <TeamUpdateDto>())).ReturnsAsync(true);
            var           target = new TeamsController(mockTeamService.Object);
            TeamUpdateDto dto    = new TeamUpdateDto();
            int           teamId = 1;

            // Act
            var result = await target.UpdateTeam(1, dto);

            //Assert
            Assert.Equal(200, result.StatusCode);
            Assert.Equal(true, result.Value);
            mockTeamService.Verify(v => v.UpdateTeamAsync(teamId, dto), Times.Once);
        }
コード例 #9
0
        public async Task <bool> UpdateTeamAsync(int teamId, TeamUpdateDto dto)
        {
            var existingTeam = await this.repository.GetTeamAsync(teamId);

            if (existingTeam == null)
            {
                throw new NotFoundException("Team not found");
            }
            if (existingTeam.Name != dto.Name)
            {
                if (await this.repository.TeamNameExistsAsync(dto.Name).ConfigureAwait(false))
                {
                    throw new BadRequestException("Team name already exists.");
                }
            }

            await repository.UpdateTeamAsync(teamId, dto.Name, dto.Zip).ConfigureAwait(false);

            return(true);
        }
コード例 #10
0
        public async void TeamService_UpdateTeamAsync_CallsRepositoryUpdateTeam()
        {
            // arrange
            Mock <IRepository> mockRepo = new Mock <IRepository>();

            mockRepo.Setup(r => r.GetTeamAsync(It.IsAny <int>())).ReturnsAsync(new TeamDto());
            TeamService   target = new TeamService(mockRepo.Object);
            int           teamId = 1;
            TeamUpdateDto dto    = new TeamUpdateDto
            {
                Name = "Anne Telohps",
                Zip  = "90210"
            };

            // act
            var result = await target.UpdateTeamAsync(teamId, dto);

            // assert
            mockRepo.Verify(r => r.UpdateTeamAsync(teamId, dto.Name, dto.Zip), Times.Once);
        }
コード例 #11
0
        public IActionResult UpdateTeam(int id, [FromBody] TeamUpdateDto teamUpdate)
        {
            string userGuid = UserHelper.GetUserGuid(_httpContextAccessor);
            var    getUser  = _transActionRepo.GetUsers().FirstOrDefault(c => c.Guid == userGuid);

            var user = _transActionRepo.GetCurrentUser(getUser.Guid);

            if (user.UserId == teamUpdate.UserId || user.Role.Name.ToLower() == "admin")
            {
                var teamEntity = _transActionRepo.GetTeam(id);
                if (teamEntity == null)
                {
                    return(NotFound());
                }
                if (teamUpdate == null)
                {
                    return(NotFound());
                }

                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }
                _mapper.Map(teamUpdate, teamEntity);
                if (!_transActionRepo.Save())
                {
                    return(StatusCode(500, "A problem happened while handling your request."));
                }

                return(GetTeamById(id));
            }
            else
            {
                return(BadRequest());
            }
        }
コード例 #12
0
        public async Task <ObjectResult> UpdateTeam(int teamId, [FromBody] TeamUpdateDto dto)
        {
            var result = await teamsService.UpdateTeamAsync(teamId, dto);

            return(new OkObjectResult(result));
        }
コード例 #13
0
ファイル: TeamController.cs プロジェクト: SutoZ/FormulaRace
 public async Task UpdateTeam(int id, [FromBody] TeamUpdateDto updateDto)
 {
     await teamService.UpdateTeamAsync(id, updateDto);
 }
コード例 #14
0
ファイル: TeamService.cs プロジェクト: SutoZ/FormulaRace
 public async Task UpdateTeamAsync(int id, TeamUpdateDto updateDto)
 {
     await repository.UpdateTeamAsync(id, updateDto);
 }