public Album CreateAlbum(Album album) { album = context.Albums.Add(album).Entity; context.SaveChanges(); return(album); }
public IHttpResponse CreateConfirm(IHttpRequest httpRequest) { if (!this.IsLoggedIn(httpRequest)) { return(this.Redirect("/Users/Login")); } using (var context = new RunesDbContext()) { var name = ((ISet <string>)httpRequest.FormData["name"]).FirstOrDefault(); var cover = ((ISet <string>)httpRequest.FormData["cover"]).FirstOrDefault(); var album = new Album { Id = Guid.NewGuid().ToString(), Name = name, Cover = cover, Price = 0m }; if (!this.IsValid(album)) { return(this.Redirect("/Albums/Create")); } context.Albums.Add(album); context.SaveChanges(); return(this.Redirect("/Albums/All")); } }
public IHttpResponse CreateConfirm(IHttpRequest httpRequest) { if (!this.IsLoggedIn(httpRequest)) { return(this.Redirect("/Users/Login")); } using (var db = new RunesDbContext()) { var name = ((ISet <string>)httpRequest.FormData["name"]).FirstOrDefault(); var cover = ((ISet <string>)httpRequest.FormData["cover"]).FirstOrDefault(); var album = new Album() { Name = name, Cover = cover, Price = 0M, }; db.Albums.Add(album); db.SaveChanges(); } return(this.Redirect("/Albums/All")); }
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 CreatePost(IHttpRequest httpRequest) { if (!this.IsLoggedIn(httpRequest)) { return(this.Redirect("/Users/Login")); } using (var context = new RunesDbContext()) { string name = ((IList <string>)httpRequest.FormData["name"]).FirstOrDefault(); string cover = ((IList <string>)httpRequest.FormData["cover"]).FirstOrDefault(); Album album = new Album() { Name = name, Cover = cover, Price = 0m }; context.Albums.Add(album); context.SaveChanges(); } return(this.Redirect("/Albums/All")); }
public bool CreateUser(RegisterViewModel registerModel) { User user = new User() { Username = registerModel.Username, Id = Guid.NewGuid().ToString(), HashedPassword = this.hashService.StrongHash(registerModel.Password), Email = registerModel.Email }; using (RunesDbContext db = new RunesDbContext()) { if (db.Users.Any(u => u.Username == user.Username || u.Email == user.Email)) { return(false); } try { db.Users.Add(user); db.SaveChanges(); return(true); } catch (Exception e) { Console.WriteLine(e); throw; } } }
public IHttpResponse RegisterConfirm(IHttpRequest httpRequest) { using (var context = new RunesDbContext()) { string username = httpRequest.FormData["username"].ToString(); string password = httpRequest.FormData["password"].ToString(); string passwordConfirm = httpRequest.FormData["password"].ToString(); string email = httpRequest.FormData["email"].ToString(); if (password != passwordConfirm) { return(this.Redirect("/Users/Register")); } User user = new User() { Username = username, Password = this.HashPassword(password), Email = email }; context.Users.Add(user); context.SaveChanges(); return(this.Redirect("/Users/Login")); } }
public IHttpResponse CreateConfirm(IHttpRequest httpRequest) { if (!this.IsLogedIn(httpRequest)) { return(this.Redirect("/Users/Login")); } using (var context = new RunesDbContext()) { Album album = new Album() { Name = httpRequest.FormData["name"].ToString(), Cover = httpRequest.FormData["cover"].ToString() }; if (album == null) { return(this.Redirect("/Albums/Create")); } context.Albums.Add(album); context.SaveChanges(); } return(this.Redirect("/Albums/All")); }
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 bool Create(string albumId, string name, string link, decimal price) { using (RunesDbContext db = new RunesDbContext()) { Track track = new Track() { Id = Guid.NewGuid().ToString(), Name = name, Link = link, Price = price, }; if (!Validation.TryValidate(track)) { return(false); } AlbumTrack albumTrack = new AlbumTrack() { Track = track, AlbumId = albumId }; db.AlbumsTracks.Add(albumTrack); db.SaveChanges(); return(true); } }
public IHttpResponse CreateConfirm(IHttpRequest httpRequest) { if (!this.IsLogedIn(httpRequest)) { return(this.Redirect("/Users/Login")); } string albumId = httpRequest.QueryData["albumId"].ToString(); using (var context = new RunesDbContext()) { Track track = new Track() { Name = httpRequest.FormData["name"].ToString(), Link = httpRequest.FormData["link"].ToString(), Price = decimal.Parse(httpRequest.FormData["price"].ToString()) }; if (track == null) { return(this.Redirect($"/Tracks/Create?trackId{albumId}")); } Album albumFromDb = context.Albums.FirstOrDefault(album => album.Id == albumId); albumFromDb.Tracks.Add(track); albumFromDb.Price = ((albumFromDb.Tracks.Select(trackToSumm => trackToSumm.Price).Sum()) * 87) / 100; //context.Update(albumFromDb); context.SaveChanges(); } return(this.Redirect($"/Albums/Details?albumId={albumId}")); }
public IHttpResponse RegisterPost(IHttpRequest httpRequest) { using (var context = new RunesDbContext()) { string username = ((IList <string>)httpRequest.FormData["username"]).FirstOrDefault(); string email = ((IList <string>)httpRequest.FormData["email"]).FirstOrDefault(); string password = ((IList <string>)httpRequest.FormData["password"]).FirstOrDefault(); string confirmPassword = ((IList <string>)httpRequest.FormData["confirmPassword"]).FirstOrDefault(); if (password != confirmPassword) { return(this.Redirect("/Users/Register")); } User user = new User() { Username = username, Email = email, Password = this.HashPassword(password) }; context.Users.Add(user); context.SaveChanges(); } return(this.Redirect("/Users/Login")); }
public IHttpResponse RegisterConfirm(IHttpRequest httpRequest) { using (var context = new RunesDbContext()) { var username = ((ISet <string>)httpRequest.FormData["username"]).FirstOrDefault(); var password = ((ISet <string>)httpRequest.FormData["password"]).FirstOrDefault(); var confirmPassword = ((ISet <string>)httpRequest.FormData["confirmPassword"]).FirstOrDefault(); var email = ((ISet <string>)httpRequest.FormData["email"]).FirstOrDefault(); if (password != confirmPassword) { return(this.Redirect("/Users/Register")); } var user = new User { Id = Guid.NewGuid().ToString(), Username = username, Password = this.HashPassword(password), Email = email }; if (!this.IsValid(user)) { return(this.Redirect("/Users/Register")); } context.Users.Add(user); context.SaveChanges(); } return(this.Redirect("/Users/Login")); }
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 void Create(string name, string cover) { var album = new Album { Name = name, Cover = cover, Price = 0.00M }; db.Albums.Add(album); db.SaveChanges(); }
public void Create(string username, string password, string email) { var user = new User() { Username = username, Password = HashPassword(password), Email = email }; db.Users.Add(user); db.SaveChanges(); }
public void AddUser(string username, string password, string email) { var hashPassword = Sha256Hash(password); var user = new User { Username = username, Password = hashPassword, Email = email, }; this.db.Users.Add(user); db.SaveChanges(); }
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 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.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(); decimal price; if (!decimal.TryParse(((ISet <string>)httpRequest.FormData["price"]).FirstOrDefault(), out price)) { return(this.Redirect($"/Tracks/Create?albumId={albumId}")); } var track = new Track { Id = Guid.NewGuid().ToString(), Name = name, Link = link, Price = price, AlbumId = albumId }; if (!this.IsValid(track)) { return(this.Redirect($"/Tracks/Create?albumId={albumId}")); } albumFromDb.Tracks.Add(track); albumFromDb.Price = albumFromDb .Tracks .Sum(t => t.Price) * 0.87m; context.SaveChanges(); return(this.Redirect($"/Albums/Details?albumId={albumId}")); } }
public void AddAlbum(string name, string cover) { using (var context = new RunesDbContext()) { var album = new Album() { Name = name, Cover = cover }; context.Albums.Add(album); context.SaveChanges(); } }
public void AddTrack(string name, string link, decimal price, int albumId) { using (var context = new RunesDbContext()) { var track = new Track() { Name = name, Link = link, Price = price, AlbumId = albumId }; context.Tracks.Add(track); context.SaveChanges(); } }
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()); }
public IHttpResponse CreateConfirm(IHttpRequest httpRequest) { if (!IsLoggedIn(httpRequest)) { return(Redirect("/")); } var albumId = httpRequest.QueryData["albumId"].ToString(); var name = ((ISet <string>)httpRequest.FormData["name"]).FirstOrDefault(); var link = ((ISet <string>)httpRequest.FormData["link"]).FirstOrDefault(); var price = ((ISet <string>)httpRequest.FormData["price"]).FirstOrDefault(); using var context = new RunesDbContext(); var track = new Track { Id = Guid.NewGuid().ToString(), Name = name, Link = link, Price = decimal.Parse(price), AlbumId = albumId }; if (!IsValid(track)) { return(Redirect($"Albums/Details?id={albumId}")); } context.Tracks.Add(track); var album = context.Albums.Find(albumId); album.Price = album.Tracks.Sum(t => t.Price) * 0.87m; context.SaveChanges(); return(Redirect($"/Albums/Details?id={album.Id}")); }
public void Create(AlbumToCreateViewModel model) { using (RunesDbContext db = new RunesDbContext()) { Album album = new Album() { Id = Guid.NewGuid().ToString(), Name = model.Name, Cover = model.Cover }; try { db.Albums.Add(album); db.SaveChanges(); } catch (Exception e) { Console.WriteLine(e.Message); } } }
public void Seed(RunesDbContext db) { List <Album> albums = new List <Album>() { new Album() { Id = Guid.NewGuid().ToString(), Name = "Master Of Puppets", Cover = "https://upload.wikimedia.org/wikipedia/en/thumb/b/b2/Metallica_-_Master_of_Puppets_cover.jpg/220px-Metallica_-_Master_of_Puppets_cover.jpg" }, new Album() { Id = Guid.NewGuid().ToString(), Name = "Play the game", Cover = "https://images.eil.com/large_image/QUEEN_PLAY%2BTHE%2BGAME-291209.jpg", } }; var firstAlbumTracks = new HashSet <AlbumTrack>() { new AlbumTrack() { AlbumId = albums[0].Id, Track = new Track() { Name = "Battery", Link = "https://www.youtube.com/embed/UipTt-qqZOE", Price = 4.56M } }, new AlbumTrack() { AlbumId = albums[0].Id, Track = new Track() { Name = "Master of puppets", Link = "https://www.youtube.com/embed/xnKhsTXoKCI", Price = 6.56M } }, new AlbumTrack() { AlbumId = albums[0].Id, Track = new Track() { Name = "The thing that should not be", Link = "https://www.youtube.com/embed/DuWtFk1Lue4", Price = 4.56M } }, new AlbumTrack() { AlbumId = albums[0].Id, Track = new Track() { Name = "Sanataoritum", Link = "https://www.youtube.com/embed/WElvEZj0Ltw", Price = 4.56M } }, }; var secondAlbumTracks = new HashSet <AlbumTrack>() { new AlbumTrack() { AlbumId = albums[1].Id, Track = new Track() { Name = "Play the game", Link = "https://www.youtube.com/embed/LS1RXZ6qpLc", Price = 4.56M } }, new AlbumTrack() { AlbumId = albums[1].Id, Track = new Track() { Name = "Another one bites to dust", Link = "https://www.youtube.com/embed/rY0WxgSXdEE", Price = 4.56M } }, new AlbumTrack() { AlbumId = albums[1].Id, Track = new Track() { Name = "Spread your wings", Link = "https://www.youtube.com/embed/uyd6OLyhPJo", Price = 6.56M } }, new AlbumTrack() { AlbumId = albums[0].Id, Track = new Track() { Name = "The thing that should not be", Link = "https://www.youtube.com/embed/DuWtFk1Lue4", Price = 4.56M } } }; albums[0].AlbumTracks = firstAlbumTracks; albums[1].AlbumTracks = secondAlbumTracks; db.Albums.AddRange(albums); db.SaveChanges(); }
public User CreateUser(User user) { context.Users.Add(user); context.SaveChanges(); return(user); }
public User CreateUser(User userForDb) { userForDb = context.Users.Add(userForDb).Entity; context.SaveChanges(); return(userForDb); }
public Track CreateTrack(Track track) { context.Tracks.Add(track); context.SaveChanges(); return(track); }