public bool TradeCandy(Guid userCandyId1, Guid userCandyId2) { using (var db = new SqlConnection(_connectionString)) { var candyRepo = new CandyRepository(); var offerRepo = new OfferRepository(); var user1 = GetUserFromDatabase(userCandyId1); var user2 = GetUserFromDatabase(userCandyId2); var candy1 = candyRepo.GetCandyFromDatabase(userCandyId1); var candy2 = candyRepo.GetCandyFromDatabase(userCandyId2); var userId1 = user1.Id; var userId2 = user2.Id; var candyId1 = candy1.Id; var candyId2 = candy2.Id; offerRepo.RemoveOffer(userCandyId1); offerRepo.RemoveOffer(userCandyId2); DeleteUserCandyEntry(userCandyId1); DeleteUserCandyEntry(userCandyId2); var sql = @"INSERT INTO [UserCandy] ([Userid], [CandyId]) VALUES (@userId1, @candyId2), (@userId2, @candyId1)"; var parameters = new { userId1, userId2, candyId1, candyId2 }; return(db.Execute(sql, parameters) == 2); } }
public bool DonateCandy(Guid userIdWhoIsDonating, Guid userCandyIdToDonate) { using (var db = new SqlConnection(_connectionString)) { var candyRepo = new CandyRepository(); var offerRepo = new OfferRepository(); var candyToDonate = candyRepo.GetCandyFromDatabase(userCandyIdToDonate); var userToDonate = WhoToDonateTo(candyToDonate.Id, userIdWhoIsDonating); DeleteUserCandyEntry(userCandyIdToDonate); offerRepo.RemoveOffer(userCandyIdToDonate); var sql = @"INSERT INTO [UserCandy] ([UserId], [CandyId]) VALUES (@userId, @candyId)"; var sql2 = @"UPDATE [User] SET AmountOfCandyDonated += 1 WHERE Id = @UserId"; db.Execute(sql2, new { UserId = userIdWhoIsDonating }); return(db.Execute(sql, new { CandyId = candyToDonate.Id, UserId = userToDonate.Id }) == 1); } }