public List <BeatViewModel> GetAllBeatsByProducer(string producerId) { List <BeatViewModel> beats = new List <BeatViewModel>(); string connectionString = HostingEnvironment.ApplicationPhysicalPath + String.Format("/Producers/{0}/BeatInfo.txt", producerId); using (StreamReader sr = new StreamReader(connectionString)) { while (sr.Peek() >= 0) { BeatViewModel beat = new BeatViewModel(); string line = sr.ReadLine(); string[] values = line.Split(','); beat.BeatId = Int32.Parse(values[0]); beat.Title = values[1]; beat.LeasePrice = Int32.Parse(values[2]); beat.BuyPrice = Int32.Parse(values[3]); beat.ActiveStatus = (values[4] == "1"); beats.Add(beat); } } // flip them to descending order to display most recent beats first List <BeatViewModel> beatListSorted = beats.OrderByDescending(x => x.BeatId).ToList(); return(beatListSorted); }
public BeatViewModel GetBeatByBeatAndProducer(int beatId, string producerId) { string connectionString = HostingEnvironment.ApplicationPhysicalPath + String.Format("/Producers/{0}/BeatInfo.txt", producerId); BeatViewModel beat = new BeatViewModel(); using (StreamReader sr = new StreamReader(connectionString)) { while (sr.Peek() >= 0) { string line = sr.ReadLine(); string[] values = line.Split(','); if (values[0] == beatId.ToString()) { beat.BeatId = Int32.Parse(values[0]); beat.Title = values[1]; beat.LeasePrice = Int32.Parse(values[2]); beat.BuyPrice = Int32.Parse(values[3]); beat.ActiveStatus = (values[4] == "1"); break; } } } if (beat.ActiveStatus == false) { return(new BeatViewModel()); } return(beat); }
// GET: BuyPage public ActionResult Buy(string producerId, string orderId, int beatId) { ProducerViewModel producer = _beatProvider.GetPageInfoByProducer(producerId); BeatViewModel beat = _beatProvider.GetBeatByBeatAndProducer(beatId, producerId); TransactionViewModel data = new TransactionViewModel() { Beat = beat, Producer = producer, Order = orderId }; if ((orderId.ToLower() == "buy" || orderId.ToLower() == "lease") && beat.ActiveStatus == true) { return(View(data)); } else { return(new HttpStatusCodeResult(400, "Invalid Url. Please try again from our home page, BeatSurge.com.")); } }
public void InactivateBeat(int beatId, string producerId) { string connectionString = HostingEnvironment.ApplicationPhysicalPath + String.Format("/Producers/{0}/BeatInfo.txt", producerId); BeatViewModel beat = new BeatViewModel(); string line = ""; string oldFile = ""; // Get full file using (StreamReader fullFile = new StreamReader(connectionString)) { oldFile = fullFile.ReadToEnd(); } // Get line to update using (StreamReader sr = new StreamReader(connectionString)) { while (sr.Peek() >= 0) { line = sr.ReadLine(); string[] values = line.Split(','); if (values[0] == beatId.ToString()) { break; } } } string newLine = line; // update last character of line, which is activestatus StringBuilder sb = new StringBuilder(newLine); sb[newLine.Length - 1] = '0'; newLine = sb.ToString(); // update file with new line using (StreamWriter writer = new StreamWriter(connectionString)) { string newFile = oldFile.Replace(line, newLine); writer.Write(newFile); } return; }
public ActionResult SendTransaction(string stripeToken, string fullName, string email) { try { fullName = fullName.Replace(",", ""); email = email.Replace(",", ""); // we need to snip out the parameters from the Url string fullPath = HttpContext.Request.Url.AbsolutePath; if (fullPath.StartsWith("/")) { fullPath = fullPath.Substring(1); } string[] urlParams = fullPath.Split('/'); string producerId = urlParams[0]; string order = urlParams[1]; int beatId = Int32.Parse(urlParams[2]); BeatViewModel beat = _beatProvider.GetBeatByBeatAndProducer(beatId, producerId); // Grab the correct price (in cents) int price; if (order == "Lease" || order == "lease") { price = beat.LeasePrice * 100; } else if (order == "buy" || order == "Buy") { price = beat.BuyPrice * 100; } else { return(new HttpStatusCodeResult(500)); } var description = order + " " + beat.Title + " by " + producerId + ", customer: " + fullName + ", email: " + email; StripeConfiguration.SetApiKey(_beatProvider.GetStripeApiKey()); var charges = new StripeChargeService(); var charge = charges.Create(new StripeChargeCreateOptions { Amount = price, Currency = "usd", Description = description, SourceTokenOrExistingSourceId = stripeToken }); if (charge.Status == "succeeded") { // Format price to dollars again for storage purposes price = price / 100; _beatProvider.EnterNewPurchaseRecord(fullName, email, order, beatId, producerId, price); _beatProvider.SendBeatToEmail(email, beatId, producerId); if (order == "buy" || order == "Buy") { _beatProvider.InactivateBeat(beatId, producerId); } return(View("PurchaseSucceeded")); } else { return(View("PurchaseFailed")); } } catch (Exception e) { return(new HttpStatusCodeResult(500, e.Message)); } }