public async Task <IActionResult> CreateMatch(MatchdayForCreationDto matchdayForCreation, int userId) { if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } return(Ok(await _matchesService.CreateMatch(matchdayForCreation, userId))); }
public async Task <KeyValuePair <bool, string> > CreateMatch(MatchdayForCreationDto matchdayForCreation, int userId) { var group = await _unitOfWork.Groups.GetById(matchdayForCreation.GroupId); if (group == null) { return(new KeyValuePair <bool, string>(false, "Specified group doesn't exist.")); } var membership = await _unitOfWork.Memberships.GetMembershipById(userId, group.Id); if (membership == null || membership.MembershipStatus == MembershipStatus.NotMember || membership.MembershipStatus == MembershipStatus.Sent) // || membership.Role == Role.Member { return(new KeyValuePair <bool, string>(false, "You are not allowed to create match!")); } var match = _mapper.Map <Matchday>(matchdayForCreation); // We check if selected location exists // If it doesn't we create a new one with specified country and city var location = await _unitOfWork.Locations.GetByName(matchdayForCreation.Name); if (location == null) { location = new Location { Name = matchdayForCreation.Location, CityId = matchdayForCreation.CityId, CountryId = matchdayForCreation.CountryId }; _unitOfWork.Locations.Add(location); } match.Location = location; match.Group = group; _unitOfWork.Matchdays.Add(match); if (await _unitOfWork.Complete()) { return(new KeyValuePair <bool, string>(true, "Matchday has been successfully created.")); } return(new KeyValuePair <bool, string>(false, "Matchday has not been created.")); }