Beispiel #1
0
        public async Task <IActionResult> AddOrEdit(SongModel songModel, bool isSuccess = false, int songId = 0)
        {
            if (songModel.Id == 0)
            {
                var userId = _userManager.GetUserId(this.HttpContext.User);
                var user   = await _userManager.GetUserAsync(HttpContext.User);

                songModel.UserId = userId;
                if (ModelState.IsValid)
                {
                    if (songModel.CoverPhoto != null)
                    {
                        string folder = "songs/cover/";
                        songModel.CoverImageUrl = await UploadImage(folder, songModel.CoverPhoto);
                    }

                    if (songModel.SongFile != null)
                    {
                        string folder = "songs/songfiles/";
                        songModel.SongUrl = await UploadImage(folder, songModel.SongFile);
                    }
                    int id = await _songRepository.AddNewSong(songModel);

                    if (id > 0)
                    {
                        TempData["Alert"]  = true;
                        TempData["SongID"] = id;
                    }

                    // Mail sending

                    //#region Mail

                    MailMessage msg = new MailMessage                    // instance Mail sender service
                    {
                        From = new MailAddress("*****@*****.**"), // Server Email Address
                    };

                    var subscribers = _songRepository.GetSubscriber(userId);
                    if (subscribers.Count() > 0)
                    {
                        foreach (var subscriber in subscribers)
                        {
                            msg.To.Add(subscriber.SubscribeUserEmail); // receiver Email

                            msg.Subject = "EarTube - New Song Upload";
                            msg.Body    = $"Hello {subscriber.SubscriberFirstName} {subscriber.SubscriberLastName}, {user.FirstName}-{user.LastName}  as added a new song. Go check it out"; // Message Body
                        }
                        SmtpClient client = new SmtpClient
                        {
                            Host = "smtp.gmail.com"
                        };
                        NetworkCredential credential = new NetworkCredential
                        {  // Server Email credentisal
                            UserName = "******",
                            Password = "******"
                        };
                        client.Credentials = credential;
                        client.EnableSsl   = true;
                        client.Port        = 587;
                        client.Send(msg);
                    }


                    return(Json(new { isValid = true, html = Helper.RenderRazorViewToString(this, "_ViewAll", _songRepository.GetAllSongs()) }));
                }
                return(Json(new { isValid = false, html = Helper.RenderRazorViewToString(this, "AddOrEdit", songModel) }));
            }
            else
            {
                var songFromDb = await _songRepository.GetSongById(songModel.Id);

                if (songModel.CoverPhoto != null)
                {
                    string folder = "songs/cover/";
                    songModel.CoverImageUrl = await UploadImage(folder, songModel.CoverPhoto);
                }
                else
                {
                    songModel.CoverImageUrl = songFromDb.CoverImageUrl;
                }

                if (songModel.SongFile != null)
                {
                    string folder = "songs/songfiles/";
                    songModel.SongUrl = await UploadImage(folder, songModel.SongFile);
                }
                else
                {
                    songModel.SongUrl = songFromDb.SongUrl;
                }
                int result = await _songRepository.EditSong(songModel);

                if (result > 0)
                {
                    return(Json(new { isValid = true, html = Helper.RenderRazorViewToString(this, "GetAllSongs", _songRepository.GetAllSongs()) }));
                }
            }
            return(Json(new { isValid = false, html = Helper.RenderRazorViewToString(this, "AddOrEdit", songModel) }));
        }