public async Task <IActionResult> PutPersonalFriendship(Guid id, V1DTO.FriendshipDTO friendshipDTO) { // Don't allow wrong data if (id != friendshipDTO.Id) { return(BadRequest(new V1DTO.MessageDTO("id and friendship.id do not match"))); } // Find pending friendship, don't allow if not found // // TODO: Change AppUser2Id name to FriendId in DTO var friendship = await _bll.Friendships.GetForUserPendingAsync(User.UserGuidId(), friendshipDTO.AppUser2Id); if (friendship == null) { _logger.LogError($"EDIT. No such friendship: {friendshipDTO.Id}, user: {User.UserGuidId()}"); return(NotFound(new V1DTO.MessageDTO($"No Friendship found for id {id}"))); } // Don't allow if current user has no rights to confirm this friendship (not the addressee!) // if (friendship.AppUser2Id != User.UserGuidId()) // { // return BadRequest(new V1DTO.MessageDTO($"User {User.UserGuidId().ToString()} cannot confirm this friendship")); // } // Update existing friendship (status to confirmed) await _bll.Friendships.UpdateAsync(_mapper.Map(friendshipDTO), User.UserGuidId()); await _bll.SaveChangesAsync(); return(NoContent()); }
public async Task <ActionResult <V1DTO.FriendshipDTO> > PostPersonalFriendship(V1DTO.FriendshipDTO friendshipDTO) { // Don't allow creating a confirmed friendship (only pending ones can be created) TODO: Remove from request DTO if (friendshipDTO.IsConfirmed) { return(BadRequest(new V1DTO.MessageDTO($"Cannot add already confirmed friendship {friendshipDTO.Id}"))); } // // Don't allow re-creating an existing friendship // var existingFriendship = await _bll.Friendships.GetForUserConfirmedAsync(User.UserGuidId(),friendshipDTO.AppUser2Id); // var pendingFriendship = await _bll.Friendships.GetForUserPendingAsync(User.UserGuidId(),friendshipDTO.AppUser2Id); // if (existingFriendship != null || pendingFriendship != null) // { // var friendshipId = existingFriendship?.Id ?? pendingFriendship.Id; // return BadRequest(new V1DTO.MessageDTO($"Cannot add already existing friendship {friendshipId}")); // } // Create pending friendship var bllEntity = _mapper.Map(friendshipDTO); await _bll.Friendships.Add(bllEntity, User.UserGuidId()); await _bll.SaveChangesAsync(); friendshipDTO.Id = bllEntity.Id; return(CreatedAtAction( "GetConfirmedFriendship", new { id = friendshipDTO.Id, version = HttpContext.GetRequestedApiVersion()?.ToString() ?? "0" }, friendshipDTO )); }