public IHttpResponse CreateConfirm(IHttpRequest httpRequest) { if (!this.IsLoggedIn(httpRequest)) { return(this.Redirect("/Users/Login")); } var albumId = httpRequest.QueryData["albumId"].ToString(); using (var context = new RunesDbContext()) { var albumFromDb = context.Albums.SingleOrDefault(album => album.Id == albumId); if (albumFromDb == null) { return(this.Redirect("/Albums/All")); } var name = ((ISet <string>)(httpRequest.FormData["name"])).FirstOrDefault(); var link = ((ISet <string>)(httpRequest.FormData["link"])).FirstOrDefault(); var price = ((ISet <string>)(httpRequest.FormData["price"])).FirstOrDefault(); Track trackForDb = new Track { Name = name, Link = link, Price = decimal.Parse(price) }; albumFromDb.Tracks.Add(trackForDb); albumFromDb.Price = (albumFromDb.Tracks.Select(track => track.Price).Sum() * 87) / 100; context.Update(albumFromDb); context.SaveChanges(); } return(this.Redirect($"/Albums/Details?id={albumId}")); }
public IHttpResponse CreateConfirm(IHttpRequest httpRequest) { if (!this.IsLoggedIn(httpRequest)) { return(this.Redirect("../Users/Login")); } var albumId = httpRequest.QueryData["albumId"].ToString(); using (var db = new RunesDbContext()) { string name = ((ISet <string>)httpRequest.FormData["name"]).FirstOrDefault(); string link = ((ISet <string>)httpRequest.FormData["link"]).FirstOrDefault(); decimal price = decimal.Parse(((ISet <string>)httpRequest.FormData["price"]).FirstOrDefault()); var albumFromDb = db.Albums.Find(albumId); var albumPrice = db.Tracks.Where(track => track.Album == albumFromDb).Sum(track => track.Price); Track trackForDb = new Track { Album = albumFromDb, Name = name, Link = link, Price = price }; this.ViewData["AlbumId"] = albumId; albumFromDb.Tracks.Add(trackForDb); db.Tracks.Add(trackForDb); db.Update(albumFromDb); db.SaveChanges(); albumFromDb.Price = (albumPrice * 87) / 100; db.Update(albumFromDb); db.SaveChanges(); } return(this.Redirect($"../Albums/Details?id={albumId}")); }
public IHttpResponse CreatePost(IHttpRequest httpRequest) { if (!this.IsLoggedIn(httpRequest)) { return(this.Redirect("/Users/Login")); } string albumId = ((IList <string>)httpRequest.QueryData["albumId"])[0]; using (var context = new RunesDbContext()) { Album albumFromDb = context.Albums.Include(a => a.Tracks).SingleOrDefault(album => album.Id == albumId); if (albumFromDb == null) { return(this.Redirect("/Albums/All")); } string name = ((IList <string>)httpRequest.FormData["name"]).FirstOrDefault(); string link = ((IList <string>)httpRequest.FormData["link"]).FirstOrDefault(); string price = ((IList <string>)httpRequest.FormData["price"]).FirstOrDefault(); string decodedLink = WebUtility.UrlDecode(link); if (decodedLink.StartsWith("https://www.youtube.com/watch")) { string pattern = @"v=(.+)&*"; Regex regex = new Regex(pattern); var match = regex.Match(decodedLink); var videoId = match.Value.Replace("v=", ""); link = $"https://www.youtube.com/embed/{videoId}"; } Track track = new Track() { Name = name, Link = link, Price = decimal.Parse(price) }; albumFromDb.Tracks.Add(track); albumFromDb.Price = 0.87m * albumFromDb.Tracks.Select(t => t.Price).Sum(); context.Tracks.Add(track); context.Update(albumFromDb); context.SaveChanges(); } return(this.Redirect($"/Albums/Details?id={albumId}")); }
public bool AddTrackToAlbum(string albumId, Track trackForDb) { var album = this.GetAlbumById(albumId); if (album == null) { return(false); } album.Tracks.Add(trackForDb); album.Price = (album.Tracks.Select(track => track.Price).Sum() * 87) / 100; context.Update(album); context.SaveChanges(); return(true); }
public IHttpResponse CreateConfirm(IHttpRequest httpRequest) { if (!this.IsLoggedIn(httpRequest)) { return(this.Redirect("/Users/Login")); } string albumId = httpRequest.QueryData["albumId"].ToString(); using (var context = new RunesDbContext()) { Album albumFromDb = context.Albums .Include(a => a.Tracks) .FirstOrDefault(a => a.Id == albumId); if (albumFromDb == null) { return(this.Redirect("/Albums/All")); } string name = ((ISet <string>)httpRequest.FormData["name"]).FirstOrDefault(); string link = ((ISet <string>)httpRequest.FormData["link"]).FirstOrDefault(); string price = ((ISet <string>)httpRequest.FormData["price"]).FirstOrDefault(); Track track = new Track() { Name = name, Link = link, Price = decimal.Parse(price), }; decimal reducePriceBy13Percent = 0.87m; albumFromDb.Tracks.Add(track); albumFromDb.Price = albumFromDb.Tracks .Select(t => t.Price) .Sum() * reducePriceBy13Percent; context.Update(albumFromDb); context.SaveChanges(); } return(this.Redirect($"/Albums/Details?id={albumId}")); }
public bool AddTrackToAlbum(string albumId, Track trackFromDb) { Album albumFromDb = this.GetAlbumById(albumId); if (albumFromDb is null) { return(false); } //we add the track albumFromDb.Tracks.Add(trackFromDb); //afterwards we correct the price of the album albumFromDb.Price = (albumFromDb.Tracks .Select(track => track.Price) .Sum() * 87) / 100; context.Update(albumFromDb); context.SaveChanges(); return(true); }
public IHttpResponse Details(IHttpRequest httpRequest) { if (!this.IsLoggedIn(httpRequest)) { return(this.Redirect("../Users/Login")); } using (var db = new RunesDbContext()) { var albumId = httpRequest.QueryData["id"].ToString(); var albumFromDb = db.Albums.Find(albumId); var albumPrice = db.Tracks.Where(track => track.Album.Id == albumFromDb.Id).Sum(track => track.Price); albumPrice = (albumPrice * 87) / 100; if (albumFromDb == null) { return(this.Redirect("All")); } //this.ViewData["Album"] = albumFromDb.AlbumsDetailsToHtml(); this.ViewData["Cover"] = WebUtility.UrlDecode(albumFromDb.Cover); this.ViewData["Price"] = $"{albumPrice:F2}"; this.ViewData["Name"] = albumFromDb.Name; this.ViewData["AlbumId"] = albumId; if (!db.Tracks.Any()) { this.ViewData["Tracks"] = "There are currently no tracks!"; } else { this.ViewData["Tracks"] = db.AlbumsDetailsToHtml(albumFromDb); } albumFromDb.Price = albumPrice; db.Update(albumFromDb); db.SaveChanges(); } return(this.View()); }