public async Task <MemberResource> CreateMember([FromBody] SaveMemberResource memberResource) { // if (!ModelState.IsValid) // return BadRequest(ModelState); var member = _mapper.Map <SaveMemberResource, Member>(memberResource); _repository.Add(member); await _unitOfWork.CompleteAsync(); //Add TeamMember if (memberResource.TeamId > 0) { var teamMember = new TeamMember { MemberId = member.Id, TeamId = memberResource.TeamId }; _teamMeberRepository.Add(teamMember); } await _unitOfWork.CompleteAsync(); member = await _repository.GetMember(member.Id); var result = _mapper.Map <Member, MemberResource>(member); return(result); }
public async Task <MemberResource> UpdateMember(int id, [FromBody] SaveMemberResource memberResource) { // if (!ModelState.IsValid) // return BadRequest(ModelState); var member = await _repository.GetMember(id); // if (member == null) // return NotFound(); _mapper.Map <SaveMemberResource, Member>(memberResource, member); //Find LeagueTeamMember var leagueTeamMembers = member.TeamMembers .Where(tm => tm.DeletedAt == null && tm.TeamId > 0 && tm.Team.LeagueId == memberResource.LeagueId) .FirstOrDefault(); if (memberResource.TeamId > 0) { if (leagueTeamMembers == null) //Add League TeamMember { var teamMember = new TeamMember { MemberId = member.Id, TeamId = memberResource.TeamId }; _teamMeberRepository.Add(teamMember); } else //Update League Team Member { leagueTeamMembers.TeamId = memberResource.TeamId; } } else //Remove exist TeamMember when TeamId is none { if (leagueTeamMembers != null) { leagueTeamMembers.DeletedAt = DateTime.Now; } } await _unitOfWork.CompleteAsync(); member = await _repository.GetMember(member.Id); var result = _mapper.Map <Member, MemberResource>(member); return(result); }