/// <summary> /// Creates a new listing based on user input /// </summary> /// <param name="details"></param> /// <returns></returns> public static string Listing(ListingDetails details) { string response = ""; using (var db = new TBDataModel()) { int pId = (from b in db.Products where b.ISBN == details.ISBN && b.Title == details.Name select b.Id).FirstOrDefault(); // If this product doesnt exist if (pId == 0) { var newProd = new Product(); newProd.Author = details.Author; newProd.ISBN = details.ISBN; newProd.Publisher = details.Publisher; newProd.Title = details.Name; newProd.UserId = details.UserId; db.Products.Add(newProd); db.SaveChanges(); pId = newProd.Id; } // Make new listing var newListing = new Transaction(); newListing.Active = 1; newListing.DateCreated = DateTime.UtcNow; newListing.DateModified = DateTime.UtcNow; newListing.Description = details.Description; newListing.Price = details.ListPrice; newListing.ProductId = pId; newListing.UserId = details.UserId; db.Transactions.Add(newListing); // Add the images uploaded to the database foreach (var im in details.Images) { var image = new Image(); image.ImageData = Convert.FromBase64String(im); image.ProductId = pId; image.UserId = details.UserId; db.Images.Add(image); } db.SaveChanges(); Dictionary <string, string> r = new Dictionary <string, string>(); r.Add("NewListingId", newListing.Id.ToString()); response = JsonConvert.SerializeObject(r); } return(response); }
public static string Update(UpdateUser details) { ResponseData r = new ResponseData(); using (var db = new TBDataModel()) { // Get the user we want to edit var uToChange = (from b in db.Users where b.Id == details.UIdToChange select b).FirstOrDefault(); // If the hashed password equals the stored hash, then we are good to update info if (uToChange.PasswordHash == details.PasswordHash) { if (details.NewPwHash != null && details.NewPwHash != "") { uToChange.PasswordHash = details.NewPwHash; } } if (details.EmailAddress != null && details.EmailAddress != "") { uToChange.EmailAddress = details.EmailAddress; } if (details.PhoneNumber != null && details.PhoneNumber != "") { uToChange.PhoneNumber = details.PhoneNumber; } if (details.FirstName != null && details.FirstName != "") { uToChange.FirstName = details.FirstName; } if (details.LastName != null && details.LastName != "") { uToChange.LastName = details.LastName; } db.SaveChanges(); } r.Status = 1; r.Data.Add("Result", "Operation Complete"); return(JsonConvert.SerializeObject(r)); }
public static string Accept(AcceptPurchase details) { ResponseData r = new ResponseData(); // Seller marked a sale as complete // API sets the transaction as inactive using (var db = new TBDataModel()) { var chosenBid = (from b in db.Bids where b.Id == details.BidId select b).FirstOrDefault(); // If the sale doesnt exist, then it doesnt exist if (chosenBid.Transaction == null || chosenBid.Transaction.Active == 0) { throw new Exception("Transaction does not exist."); } // if the sale exists, set the sale to inactive to mark as complete chosenBid.Transaction.Active = 0; chosenBid.Transaction.DateModified = DateTime.UtcNow; // set Accepted flag to true the bids associated with the item // But only if there are no others if (db.Bids.Any(x => x.Accepted == 1 && x.TransactionId == chosenBid.TransactionId)) { // if a bid has already been accepted throw new Exception("There is already a bid accepted for this transaction."); } chosenBid.Accepted = 1; db.SaveChanges(); r.Status = 1; r.Schema = "AcceptPurchase"; r.Data.Add("Purchase Status", "Accepted"); } return(JsonConvert.SerializeObject(r)); }
/// <summary> /// Creates a new user based on the passed in 'details' parameter /// </summary> /// <param name="details"></param> /// <returns></returns> public static string User(NewUser details) { ResponseData r = new ResponseData(); using (var db = new TBDataModel()) { // Check to see if the username already exists. If it does, return an error var usernameId = (from b in db.Users where b.UserName == details.UserName select b.Id).FirstOrDefault(); // If this evaluates to true, then the username is already in use if (usernameId != 0) { Dictionary <string, string> error = new Dictionary <string, string>(); error.Add("Error", "Username already exists."); error.Add("ErrorCode", "1"); return(JsonConvert.SerializeObject(error)); } // If the username is okay, add data to db User newUser = new User(); newUser.PasswordHash = details.PasswordHash; newUser.PhoneNumber = details.PhoneNumber; newUser.UserName = details.UserName; newUser.EmailAddress = details.EmailAddress; db.Users.Add(newUser); // Save user Changes db.SaveChanges(); r.Status = 1; r.Schema = "NewUser"; r.Data.Add("UserId", newUser.Id.ToString()); r.Data.Add("UserName", newUser.UserName); } return(JsonConvert.SerializeObject(r)); }
public static string Make(BidData details) { ResponseData r = new ResponseData(); using (var db = new TBDataModel()) { // using the bid details // Add new bid to database EntityFramework.Bid newBid = new EntityFramework.Bid(); newBid.ProposedPrice = details.ProposedPrice; newBid.TransactionId = details.TransactionId; newBid.UserId = details.PurchaserId; newBid.Accepted = 0; db.Bids.Add(newBid); db.SaveChanges(); r.Status = 1; r.Schema = "NewBid"; r.Data.Add("BidId", newBid.Id.ToString()); } return(JsonConvert.SerializeObject(r)); }