/// <summary> /// Adds a title to a user's collection and reduces their points appropriately. /// </summary> /// <param name="userId"></param> /// <param name="storeItemId"></param> /// <returns></returns> public async Task <ServiceResult> BuyTitleAsync(string userId, int storeItemId) { var title = await db.StoreItems.FindAsync(storeItemId); var user = db.Users.Find(userId); // get any sales that this item is under int pointsCost = await GetAdjustedSalePrice(storeItemId, title.PointPrice); bool success = false; List <string> errors = new List <string>(); if (user.CurrentPoints >= pointsCost) { try { UserTitle userTitle = new UserTitle() { UserId = userId, StoreItemId = storeItemId, DateObtained = DateTime.Now }; // if the user does not own the title, let them purchase it bool doesUserAlreadyHaveTitle = await db.UserTitles.AnyAsync(a => a.StoreItemId == storeItemId && a.UserId == userId); if (!doesUserAlreadyHaveTitle) { db.UserTitles.Add(userTitle); LogStoreTransaction(userId, storeItemId, 1, pointsCost); user.CurrentPoints -= pointsCost; success = (await db.SaveChangesAsync()) > 0; } else { errors.Add(ErrorMessages.TitleAlreadyObtained); ValidationDictionary.AddError(Guid.NewGuid().ToString(), ErrorMessages.TitleAlreadyObtained); } } catch (DbUpdateException ex) { Log.Error("StoreService.BuyGiftAsync", ex, new { userId, storeItemId }); errors.Add(ErrorMessages.TitleAlreadyObtained); } if (success) { await AwardAchievedMilestonesForUserAsync(userId, (int)MilestoneTypeValues.TitlesPurchased); } } else { string error = String.Format(ErrorMessages.UserDoesNotHaveEnoughPointsToPurchase, 1, title.Name); errors.Add(error); ValidationDictionary.AddError(Guid.NewGuid().ToString(), error); } return(success ? ServiceResult.Success : ServiceResult.Failed(errors.ToArray())); }
/// <summary> /// Adds a gift to a user's collection and reduces their points appropriately. /// </summary> /// <param name="userId"></param> /// <param name="storeItemId"></param> /// <param name="buyCount"></param> /// <returns></returns> public async Task <ServiceResult> BuyGiftAsync(string userId, int storeItemId, int buyCount) { Debug.Assert(!String.IsNullOrEmpty(userId)); Debug.Assert(storeItemId > 0); Debug.Assert(buyCount > 0); // check price of item and see if the user has enough points to buy the item with the count var gift = await db.StoreItems.FindAsync(storeItemId); var user = db.Users.Find(userId); // get any sales that this item is under int pointsCost = await GetAdjustedSalePrice(storeItemId, gift.PointPrice); bool success = false; string failMessage = String.Empty; if (user.CurrentPoints >= pointsCost * buyCount) { try { // check if user already has an item of this type var inventoryItem = await(from inventory in db.InventoryItems where inventory.StoreItemId == storeItemId where inventory.ApplicationUserId == userId select inventory).FirstOrDefaultAsync(); // if they do, increase the count of their item based on buyCount if (inventoryItem != null) { inventoryItem.ItemCount += buyCount; } // if they don't, create a new item and add it to their inventory else { InventoryItem item = new InventoryItem() { ApplicationUserId = userId, StoreItemId = storeItemId, ItemCount = buyCount }; db.InventoryItems.Add(item); } LogStoreTransaction(userId, storeItemId, buyCount, pointsCost); user.CurrentPoints -= pointsCost * buyCount; success = (await db.SaveChangesAsync()) > 0; } catch (DbUpdateException ex) { Log.Error("StoreService.BuyGiftAsync", ex, new { userId, storeItemId, buyCount }); ValidationDictionary.AddError(Guid.NewGuid().ToString(), ErrorMessages.GiftPurchaseFailed); } if (success) { await AwardAchievedMilestonesForUserAsync(userId, (int)MilestoneTypeValues.GiftsPurchased); } else { failMessage = ErrorMessages.GiftPurchaseFailed; } } else { failMessage = String.Format(ErrorMessages.UserDoesNotHaveEnoughPointsToPurchase, buyCount, gift.Name); ValidationDictionary.AddError(Guid.NewGuid().ToString(), failMessage); } return(success ? ServiceResult.Success : ServiceResult.Failed(failMessage)); }