public ICollection <Models.BridgeModel.ArtistSongModel> RandomArtistToSong(ICollection <SongModel> songs, ICollection <ArtistModel> artists) { ICollection <Models.BridgeModel.ArtistSongModel> ret = new List <Models.BridgeModel.ArtistSongModel>(); for (int i = 0; i < songs.Count; i++) { int randomNumArtists = random.Next(1, 3); for (int j = 0; j < randomNumArtists; j++) { Models.BridgeModel.ArtistSongModel temp = new Models.BridgeModel.ArtistSongModel(); temp.SongId = songs.ElementAt(i).SongId; int rand = random.Next(0, artists.Count); temp.UserId = artists.ElementAt(rand).UserId; if (!CheckIfArtistAlreadyHaveSong(temp.UserId, temp.SongId)) { ret.Add(temp); dbContext.Add(temp); } } } dbContext.SaveChanges(); return(ret); }
public async Task <IActionResult> Create([Bind("SongId,SongName,Genre,ReleaseDate")] SongModel songModel, IFormFile songImagePath, IFormFile songPath) { if (ModelState.IsValid) { songModel.SongPath = FileHelper.SaveFile(songPath, "songs", songPath.FileName); songModel.AddedDate = DateTime.UtcNow; songModel.SongImagePath = FileHelper.SaveFile(songImagePath, "images", songImagePath.FileName); _context.Add(songModel); //adds the created song to the current artist according to the session int id = HttpContext.Session.GetInt32(SessionConsts.UserId).Value; Models.BridgeModel.ArtistSongModel temp = new Models.BridgeModel.ArtistSongModel(); temp.SongId = songModel.SongId; temp.UserId = id; _context.Add(temp); await _context.SaveChangesAsync(); FacebookModel facebookModel = new FacebookModel(); await facebookModel.Post(string.Format("A new song has been added to the site -> {0}", songModel.SongName)); return(RedirectToAction(nameof(Display))); } return(View(songModel)); }