public async Task <PetOwnerResponse> AssingPetOwner(int petId, int ownerId, bool principal) { try { await _petOwnerRepository.AssignPetOwner(petId, ownerId, principal); await _unitOfWork.CompleteAsync(); PetOwner petOwner = await _petOwnerRepository.FindByPetIdAndOwnerId(petId, ownerId); return(new PetOwnerResponse(petOwner)); } catch (Exception ex) { return(new PetOwnerResponse($"An error ocurred while assigning Pet to Owner: {ex.Message}")); } }
public async Task <ChatResponse> SaveAsync(int senderId, int receiverId, int petId) { var existingSender = await _profileRepository.FindByIdAsync(senderId); var existingReceiver = await _profileRepository.FindByIdAsync(receiverId); var existingPet = await _petProfileRepository.FindById(petId); bool attended = false; if (existingSender == null) { return(new ChatResponse("Sender not found")); } if (existingReceiver == null) { return(new ChatResponse("Receiver not found")); } if (existingPet == null) { return(new ChatResponse("Pet not found")); } var possibleOwner1 = await _petOwnerRepository.FindByPetIdAndOwnerId(petId, senderId); var possibleOwner2 = await _petOwnerRepository.FindByPetIdAndOwnerId(petId, receiverId); if (possibleOwner1 == null && possibleOwner2 == null) { return(new ChatResponse("Pet not associated with the owner")); } try { bool exist = false; IEnumerable <Chat> chatsReceiver = await _chatRepository.ListBySenderId(receiverId); if (chatsReceiver != null) { chatsReceiver.ToList().ForEach(chat => { if (chat.ReceiverProfileId == senderId && chat.PetId == petId) { exist = true; } }); } Chat chat = new Chat(); if (!exist) { chat.ReceiverProfileId = senderId; chat.SenderProfileId = receiverId; chat.PetId = petId; await _chatRepository.AddAsync(chat); await _unitOfWork.CompleteAsync(); } chat.ReceiverProfileId = receiverId; chat.SenderProfileId = senderId; chat.PetId = petId; await _chatRepository.AddAsync(chat); await _unitOfWork.CompleteAsync(); return(new ChatResponse(chat)); } catch (Exception ex) { return(new ChatResponse($"An error ocurred while saving chat: {ex.Message}")); } }