Esempio n. 1
0
        public async Task <IActionResult> AddNewSong(SongModel songModel, bool isSuccess = false, int songId = 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);
                }

                ViewBag.IsSuccess = isSuccess;
                ViewBag.SongId    = songId;

                int id = await _songRepository.AddNewSong(songModel);

                if (id > 0)
                {
                    //return RedirectToAction(nameof(AddNewSong), new { isSuccess = true, songId = id });
                    TempData["Alert"] = true;

                    // Mail sending

                    //#region Mail

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

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

                        msg.Subject = "EarTube - New Song Upload";
                        msg.Body    = $"Hello {subscriber.SubscribeUserEmail}, {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);


                    ViewBag.Success = $"Email has been sent successfully to your subscriber";
                    return(RedirectToAction(nameof(GetAllSongs), new { songId = id }));
                }
            }

            return(View());
        }
Esempio n. 2
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) }));
        }
        public async Task <IActionResult> AddUpload(UploadViewModel model)
        {
            if (ModelState.IsValid)
            {
                string unique = Guid.NewGuid().ToString();

                string UniqueTransId = "Upload-" + unique;

                var upload = new UploadDoc
                {
                    Name        = model.Name,
                    Email       = model.Email,
                    TransNumber = UniqueTransId
                };

                _upload.AddUploadDoc(upload);

                string webrootPath = _hostEnvironment.WebRootPath;
                var    files       = model.FormFiles;

                var uploads = Path.Combine(webrootPath, "images");



                for (int i = 0; i < files.Count; i++)
                {
                    string fileName = files[i].FileName;
                    //var extension = Path.GetExtension(files[i].FileName);
                    using (var fileStreams = new FileStream(Path.Combine(uploads, fileName), FileMode.Create))
                    {
                        files[i].CopyTo(fileStreams);
                    }
                    var uploadFile = new UploadImage
                    {
                        UploadId  = upload.Id,
                        ImagePath = @"\images\" + fileName
                    };

                    _upload.AddUploadFile(uploadFile);
                }

                #region Mail

                MailMessage msg = new MailMessage                                        // instance Mail sender service
                {
                    From = new MailAddress("#########################################"), // Server Email Address
                };
                msg.To.Add(model.Email);                                                 // receiver Email

                msg.Subject = "SDSD Developer Test";
                msg.Body    = "Hello " + model.Name + ", these are your attachments"; // Message Body


                foreach (var filepath in files)
                {
                    string fileName = Path.GetFileName(filepath.FileName);

                    msg.Attachments.Add(new Attachment(filepath.OpenReadStream(), fileName));
                }

                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);


                ViewBag.Success = $"Email has been sent successfully to {model.Email}";
                _logger.LogInformation("User created ");

                #endregion


                if (await _upload.SaveChangesAsync())
                {
                    return(RedirectToAction(nameof(Index)));
                }

                return(View(model));
            }
            return(View(model));
        }