Esempio n. 1
0
        public async Task <IActionResult> Create([Bind("Id,Name,ArtistId,GenreId")] Song song)
        {
            if (ModelState.IsValid)
            {
                _context.Add(song);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(song));
        }
        public async Task <IActionResult> Create([Bind("Id,Name")] Artist artist)
        {
            if (ModelState.IsValid)
            {
                _context.Add(artist);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(artist));
        }
        public async Task <IActionResult> Create([Bind("Id,UserId,PostId")] CreateFavorite favorite)
        {
            if (ModelState.IsValid)
            {
                _context.Add(favorite);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["PostId"] = new SelectList(_context.Posts, "Id", "SongId", favorite.PostId);
            ViewData["UserId"] = new SelectList(_context.Users, "Id", "UserName", favorite.UserId);
            return(View(favorite));
        }
Esempio n. 4
0
        public async Task <IActionResult> Create([Bind("Id,SongId,ArtistId,ViewCount,YoutubeLink,PdfFilePath,Reviewed")] InsertPostModel insertPost, List <IFormFile> files)
        {
            var isNormalYoutubeLink = Regex.IsMatch(insertPost.YoutubeLink, "((http(s)?://)(www.)?)?youtube.com\\/watch\\?v=([^#&?]).*");

            if (ModelState.IsValid && isNormalYoutubeLink)
            {
                var uploads = Path.Combine(_webHostEnvironment.WebRootPath, "uploads");

                var post = new Post
                {
                    ArtistId  = insertPost.ArtistId,
                    SongId    = insertPost.SongId,
                    Reviewed  = false,
                    ViewCount = 0
                };

                foreach (var file in files)
                {
                    if (file.Length > 0)
                    {
                        var directory = Path.GetDirectoryName(uploads);

                        if (!Directory.Exists(directory))
                        {
                            Directory.CreateDirectory(directory);
                        }

                        var fileName = Path.GetRandomFileName();
                        fileName  = fileName.Replace(".", "a");
                        fileName += ".pdf";

                        var fullPath = Path.Combine(uploads, fileName);

                        post.PdfFilePath = fileName;

                        // If file with same name exists delete it
                        if (System.IO.File.Exists(fullPath))
                        {
                            System.IO.File.Delete(fullPath);
                        }

                        // Create new local file and copy contents of uploaded file
                        using (var localFile = System.IO.File.OpenWrite(fullPath))
                            using (var uploadedFile = file.OpenReadStream())
                            {
                                uploadedFile.CopyTo(localFile);
                            }
                    }
                }

                var videoId = insertPost.YoutubeLink.Split("=")[1];

                post.UserId        = User.FindFirstValue(ClaimTypes.NameIdentifier);
                post.YoutubeLink   = $"https://www.youtube.com/embed/{videoId}";
                post.ThumbnailLink = $"https://img.youtube.com/vi/{videoId}/0.jpg";

                _context.Add(post);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }

            ViewData["ArtistId"] = new SelectList(_context.Artists, "Id", "Id", insertPost.ArtistId);
            ViewData["SongId"]   = new SelectList(_context.Songs, "Id", "Id", insertPost.SongId);

            return(View(insertPost));
        }