public User AdjustUserCurrency(string usernameOrID, Guid currencyID, [FromBody] AdjustCurrency currencyUpdate) { UserDataModel user = null; if (!string.IsNullOrEmpty(usernameOrID)) { if (Guid.TryParse(usernameOrID, out Guid userId)) { user = ChannelSession.Settings.GetUserData(userId); } else { user = ChannelSession.Settings.GetUserDataByUsername(StreamingPlatformTypeEnum.All, usernameOrID); } } if (user == null) { var resp = new HttpResponseMessage(HttpStatusCode.NotFound) { Content = new ObjectContent <Error>(new Error { Message = $"Unable to find user: {usernameOrID}." }, new JsonMediaTypeFormatter(), "application/json"), ReasonPhrase = "User not found" }; throw new HttpResponseException(resp); } return(AdjustCurrency(user, currencyID, currencyUpdate)); }
private User AdjustCurrency(UserDataViewModel user, Guid currencyID, [FromBody] AdjustCurrency currencyUpdate) { if (!ChannelSession.Settings.Currencies.ContainsKey(currencyID)) { throw new HttpResponseException(HttpStatusCode.NotFound); } if (currencyUpdate == null) { throw new HttpResponseException(HttpStatusCode.BadRequest); } UserCurrencyViewModel currency = ChannelSession.Settings.Currencies[currencyID]; if (currencyUpdate.Amount < 0) { int quantityToRemove = currencyUpdate.Amount * -1; if (!user.HasCurrencyAmount(currency, quantityToRemove)) { // If the request is to remove currency, but user doesn't have enough, fail throw new HttpResponseException(HttpStatusCode.Forbidden); } user.SubtractCurrencyAmount(currency, quantityToRemove); } else if (currencyUpdate.Amount > 0) { user.AddCurrencyAmount(currency, currencyUpdate.Amount); } return(UserFromUserDataViewModel(user)); }
public static async Task <User> AdjustUserCurrencyAsync(uint userId, Guid currencyID, int offsetAmount) { AdjustCurrency adjustCurrency = new AdjustCurrency { Amount = offsetAmount }; return(await RestClient.PutAsync <User>($"users/{userId}/currency/{currencyID}/adjust", adjustCurrency)); }
public User AdjustUserCurrency(string username, Guid currencyID, [FromBody] AdjustCurrency currencyUpdate) { UserDataViewModel user = ChannelSession.Settings.UserData.Values.FirstOrDefault(u => u.UserName.Equals(username, StringComparison.InvariantCultureIgnoreCase)); if (user == null) { throw new HttpResponseException(HttpStatusCode.NotFound); } return(AdjustCurrency(user, currencyID, currencyUpdate)); }
public User AdjustUserCurrency(uint userID, Guid currencyID, [FromBody] AdjustCurrency currencyUpdate) { UserDataViewModel user = ChannelSession.Settings.UserData[userID]; if (user == null) { throw new HttpResponseException(HttpStatusCode.NotFound); } return(AdjustCurrency(user, currencyID, currencyUpdate)); }
private User AdjustCurrency(UserDataModel user, Guid currencyID, [FromBody] AdjustCurrency currencyUpdate) { if (!ChannelSession.Settings.Currency.ContainsKey(currencyID)) { var resp = new HttpResponseMessage(HttpStatusCode.NotFound) { Content = new ObjectContent <Error>(new Error { Message = $"Unable to find currency: {currencyID.ToString()}." }, new JsonMediaTypeFormatter(), "application/json"), ReasonPhrase = "Currency ID not found" }; throw new HttpResponseException(resp); } if (currencyUpdate == null) { var resp = new HttpResponseMessage(HttpStatusCode.BadRequest) { Content = new ObjectContent <Error>(new Error { Message = "Unable to parse currency adjustment from POST body." }, new JsonMediaTypeFormatter(), "application/json"), ReasonPhrase = "Invalid POST Body" }; throw new HttpResponseException(resp); } CurrencyModel currency = ChannelSession.Settings.Currency[currencyID]; if (currencyUpdate.Amount < 0) { int quantityToRemove = currencyUpdate.Amount * -1; if (!currency.HasAmount(user, quantityToRemove)) { var resp = new HttpResponseMessage(HttpStatusCode.Forbidden) { Content = new ObjectContent <Error>(new Error { Message = "User does not have enough currency to remove" }, new JsonMediaTypeFormatter(), "application/json"), ReasonPhrase = "Not Enough Currency" }; throw new HttpResponseException(resp); } currency.SubtractAmount(user, quantityToRemove); } else if (currencyUpdate.Amount > 0) { currency.AddAmount(user, currencyUpdate.Amount); } return(UserFromUserDataViewModel(user)); }
public User AdjustUserCurrency(string username, Guid currencyID, [FromBody] AdjustCurrency currencyUpdate) { UserDataViewModel user = ChannelSession.Settings.UserData.Values.FirstOrDefault(u => u.UserName.Equals(username, StringComparison.InvariantCultureIgnoreCase)); if (user == null) { var resp = new HttpResponseMessage(HttpStatusCode.NotFound) { Content = new ObjectContent <Error>(new Error { Message = $"Unable to find user: {username}." }, new JsonMediaTypeFormatter(), "application/json"), ReasonPhrase = "Username not found" }; throw new HttpResponseException(resp); } return(AdjustCurrency(user, currencyID, currencyUpdate)); }
public User AdjustUserCurrency(uint userID, Guid currencyID, [FromBody] AdjustCurrency currencyUpdate) { UserDataViewModel user = ChannelSession.Settings.UserData[userID]; if (user == null) { var resp = new HttpResponseMessage(HttpStatusCode.NotFound) { Content = new ObjectContent <Error>(new Error { Message = $"Unable to find user: {userID.ToString()}." }, new JsonMediaTypeFormatter(), "application/json"), ReasonPhrase = "User ID not found" }; throw new HttpResponseException(resp); } return(AdjustCurrency(user, currencyID, currencyUpdate)); }
public async Task <User> AdjustUserCurrency(string usernameOrID, Guid currencyID, [FromBody] AdjustCurrency currencyUpdate) { UserDataModel user = await UserController.GetUserData(usernameOrID); if (user == null) { var resp = new HttpResponseMessage(HttpStatusCode.NotFound) { Content = new ObjectContent <Error>(new Error { Message = $"Unable to find user: {usernameOrID}." }, new JsonMediaTypeFormatter(), "application/json"), ReasonPhrase = "User not found" }; throw new HttpResponseException(resp); } return(AdjustCurrency(user, currencyID, currencyUpdate)); }