public async Task <IActionResult> Update_Info([FromBody] UpdateUserInfoRequest model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var username = JWTtoken.GetUsernameFromToken(Request); if (username == null) { return(NotFound()); } var emailIsUsed = _userManager.Users.Any(user => user.Email.Equals(model.Email) && !user.UserName.Equals(username)); if (emailIsUsed) { return(BadRequest($"Email {model.Email} is already taken")); } var userAccount = await _userManager.FindByNameAsync(username); userAccount.Email = model.Email; userAccount.Firstname = model.FirstName; userAccount.Lastname = model.LastName; userAccount.PhoneNumber = model.PhoneNumber; var result = await _userManager.UpdateAsync(userAccount); if (result.Succeeded) { return(Ok("User updated successfully")); } return(NotFound()); }
public async Task <IActionResult> GetMe() { var accName = JWTtoken.GetUsernameFromToken(Request); if (accName == null) { return(Unauthorized()); } var account = await _userManager.FindByNameAsync(accName); if (account == null) { return(NotFound()); } var response = new UserSelfInfoResponse { Email = account.Email, Firstname = account.Firstname, Lastname = account.Lastname, PhoneNumber = account.PhoneNumber }; return(Ok(response)); }
public async Task <IActionResult> AddReview([FromBody] ReviewDto reviewData) { //reviewData.orderId here will not be sent if (reviewData.Stars < 1 || reviewData.Stars > 5) { return(BadRequest("Star evaluation missing or is in incorrect format")); } var username = JWTtoken.GetUsernameFromToken(Request); if (username == null) { return(BadRequest()); } var acc = await _accountManager.FindByNameAsync(username); var newestOrder = _ordersRepository.GetUserPurchaseHistory(acc.Id) .OrderByDescending(order => order.Date) .FirstOrDefault(); if (newestOrder == null) { return(BadRequest("Order not found")); } if (_reviewsRepository.GetById(newestOrder.Id) != null) { return(BadRequest("This order already has a review")); } Review newReview = new Review { Id = newestOrder.Id, Description = reviewData.Description, Stars = reviewData.Stars }; Review addedReview = _reviewsRepository.Add(newReview); if (addedReview == null) { return(BadRequest("Review could not be added")); } return(Ok("Review added successfully")); }
public async Task <IActionResult> GetPurchaseHistory() { var username = JWTtoken.GetUsernameFromToken(Request); if (username == null) { return(NotFound()); } var user = await _accountManager.FindByNameAsync(username); if (user == null) { return(NotFound()); } var list = _ordersRepository.GetUserPurchaseHistory(user.Id).ToList(); if (list.Count() != 0) { List <ShoppingHistoryItemResponse> purchases = list.Select(order => new ShoppingHistoryItemResponse { Id = order.Id, Date = order.Date, Items = order.Items, Cost = order.Cost }) .ToList(); return(Ok(purchases)); } else { return(NotFound("Purchase history is empty!")); } }
public async Task <IActionResult> Change_Password([FromBody] ChangePasswordRequest request) { if (string.IsNullOrEmpty(request.CurrentPassword) || string.IsNullOrEmpty(request.NewPassword)) { return(BadRequest("Please fill all the fields")); } var username = JWTtoken.GetUsernameFromToken(Request); if (username == null) { return(NotFound()); } var user = await _userManager.FindByNameAsync(username); var result = await _userManager.ChangePasswordAsync(user, request.CurrentPassword, request.NewPassword); if (result.Succeeded) { return(Ok("Password changed successfully")); } return(BadRequest(result.Errors.First().Description)); }
public IActionResult HandlePurchase([FromBody] PaymentRequest paymentInfo) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } //validating body if (paymentInfo.Amount <= 0) { return(BadRequest("Amount to pay must is invalid")); //change request code } if (!ValidCreditCardNumber(paymentInfo.Number)) { return(BadRequest("Credit card info is invalid")); } if (paymentInfo.Holder.Length < 2 || paymentInfo.Holder.Length > 32) { return(BadRequest("Card holder name is invalid")); } if (paymentInfo.Exp_Year < 1970) { return(BadRequest("Card expiration year is invalid")); } if (paymentInfo.Exp_Month < 1 || paymentInfo.Exp_Month > 12) { return(BadRequest("Card expiration month is invalid")); } if (!ValidCvv(paymentInfo.Cvv)) { return(BadRequest("Card cvv security code is invalid")); } //get user for further information var username = JWTtoken.GetUsernameFromToken(Request); var user = _ordersRepository.GetOrderingUser(username); //check if payment ammount matches shopping cart total amount decimal ammount = (decimal)paymentInfo.Amount / 100; if (!_ordersRepository.PaymentEqualsShoppingCartSum(user, ammount)) { return(BadRequest("Payment ammount does not match the shopping cart total ammount")); } //Get current user items in the shopping cart var items = _ordersRepository.GetShoppingCartItems(user); //make payment //*****make payment***** var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://mock-payment-processor.appspot.com/v1/payment"); httpWebRequest.Credentials = new NetworkCredential("technologines", "platformos"); httpWebRequest.ContentType = "application/json"; httpWebRequest.Method = "POST"; using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream())) { string json = Newtonsoft.Json.JsonConvert.SerializeObject(paymentInfo); streamWriter.Write(json); streamWriter.Flush(); streamWriter.Close(); } try { var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse(); PaymentResponse paymentResponseInfo = new PaymentResponse(); using (var streamReader = new StreamReader(httpResponse.GetResponseStream())) { var result = streamReader.ReadToEnd(); var paymentResponseJson = JObject.Parse(result); paymentResponseInfo.Created_At = (DateTime)paymentResponseJson["created_at"]; paymentResponseInfo.Id = (string)paymentResponseJson["id"]; } //*****payment made****** //create new order with those items list Order newOrder = new Order { Cost = ammount, Date = DateTime.Now, UserId = user.Id, OrderedItem = items, PaymentId = paymentResponseInfo.Id, PaymentDate = DateTime.Now }; //save order to the database newOrder = _ordersRepository.Add(newOrder); //add new orders id to the ordered items foreach (var orderedItem in items) { orderedItem.OrderId = newOrder.Id; } //clear shopping cart _ordersRepository.ClearUserShoppingCart(user); if (newOrder.PaymentId != null) { _ordersRepository.ClearUserShoppingCart(user); return(Ok("Purchase successful")); } } catch (Exception ex) { return(BadRequest("Payment went wrong")); } return(BadRequest("Payment went wrong")); }