/** * Remove reservedGift - in case the reservation was cancelled. * Changes gift status to Active. ReservedGift will be deleted. * * @param userId is mandatory and represents current user's Id */ public async Task <BLLAppDTO.GiftBLL> CancelReservationAsync(BLLAppDTO.ReservedGiftBLL entity, Guid userId) { var targetGiftId = entity.GiftId; var giftGiverId = userId; var giftReceiverId = entity.UserReceiverId; // UserIds are mandatory for deleting ReservedGift if (giftGiverId == null || giftReceiverId == null) { throw new ArgumentNullException(nameof(userId)); } // Check gift is Reserved and by current user var gift = await UOW.Gifts.FirstOrDefaultAsync(targetGiftId, giftReceiverId); if (!(await IsGiftReservedByRequesterAsync(Mapper.Map(gift), giftGiverId))) { throw new NotSupportedException( $"Could not find a reserved gift {targetGiftId.ToString()} to cancel reservation by user {giftGiverId.ToString()}"); } // DELETE corresponding ReservedGift var reservedGift = await UOW.ReservedGifts.GetByGiftId(targetGiftId, giftGiverId); if (reservedGift == null) { throw new NotSupportedException($"Could not find reserved Gift {targetGiftId.ToString()} to cancel reservation"); } // gift = Mapper.Map(reservedGift.Gift); // Error InvalidOperationException - maybe a workaround? Right now include removed from Repo await UOW.ReservedGifts.RemoveAsync(reservedGift, giftGiverId); // UPDATE corresponding Gift's status back to Active var reactivatedGift = await UpdateGiftStatusToActiveAsync(Mapper.Map(gift), giftReceiverId); return(reactivatedGift); }
/** * Updates Gift status to Reserved and adds a new ReservedGift table entry. * * @param userId is mandatory and represents current user's Id, will be used as reserverUserId. */ public async Task <BLLAppDTO.GiftBLL> MarkAsReservedAsync(BLLAppDTO.ReservedGiftBLL entity, Guid userId) { var targetGiftId = entity.GiftId; var giftReceiverId = entity.UserReceiverId; // UserIds are mandatory for adding ReservedGift if (userId == null || giftReceiverId == null) { throw new ArgumentNullException(nameof(userId)); } // Check if this gift even exists var existingGift = Mapper.Map(await UOW.Gifts.FirstOrDefaultAsync(targetGiftId, giftReceiverId)); if (existingGift == null) { throw new NotSupportedException($"Could not reserve a gift that does not exist"); } // Check existing gift is not already reserved if (await IsGiftReservedByRequesterAsync(existingGift, userId)) { throw new NotSupportedException($"Could not reserve gift - {targetGiftId.ToString()} is already reserved"); } // ADD NEW ReservedGift var newReservedGift = AddReservedGift(targetGiftId, userId, giftReceiverId); if (newReservedGift == null) { throw new NotSupportedException($"Could not reserve gift {targetGiftId.ToString()} - data insertion fail"); } // UPDATE Gift to reserved status var updatedGift = await UpdateGiftStatusToReservedAsync(existingGift, newReservedGift, userId); return(updatedGift); }
/** * Remove reservedGift - in case it was gifted. * Inserts new corresponding archivedGift. Gift entry will be kept too, but changed to Archived status. * ReservedGift will be deleted. * * @param userId is mandatory and represents current user's Id */ public async Task <BLLAppDTO.GiftBLL> MarkAsGiftedAsync(BLLAppDTO.ReservedGiftBLL entity, Guid userId) { var targetGiftId = entity.GiftId; var giftReceiverId = entity.UserReceiverId; // UserIds are mandatory for archiving reserved gift if (userId == null || giftReceiverId == null) { throw new ArgumentNullException(nameof(userId)); } // Check that target gift exists and status is reserved (by current user) var gift = Mapper.Map(await UOW.Gifts.FirstOrDefaultAsync(targetGiftId, giftReceiverId)); var isGiftAlreadyArchived = await IsGiftArchivedAsync(gift, userId, giftReceiverId); var isGiftReservedByRequester = await IsGiftReservedByRequesterAsync(gift, userId); if (gift == null || isGiftAlreadyArchived || !isGiftReservedByRequester) { throw new NotSupportedException( $"Could not find reserved Gift {targetGiftId.ToString()} to mark as Gifted by user {userId.ToString()}"); } // ADD NEW ArchivedGift var newArchivedGift = AddArchivedGift(targetGiftId, userId, giftReceiverId); if (newArchivedGift == null) { throw new NotSupportedException($"Could not mark gift {targetGiftId.ToString()} as gifted - data insertion fail"); } // DELETE corresponding ReservedGift var reservedGift = await UOW.ReservedGifts.GetByGiftId(targetGiftId, userId); if (reservedGift == null) { throw new NotSupportedException($"Could not find reserved Gift {targetGiftId.ToString()} to mark it as gifted"); } // gift = Mapper.Map(reservedGift.Gift); // Error InvalidOperationException - maybe a workaround? Right now include removed from Repo await UOW.ReservedGifts.RemoveAsync(reservedGift, userId); // UPDATE corresponding Gift's status to Archived var updatedGift = await UpdateGiftStatusToArchivedAsync(gift, newArchivedGift, userId); return(updatedGift); }