Пример #1
0
        public async Task <IActionResult> Mark(int contributionId = -1,
                                               ContributionStatus contributionStatus = ContributionStatus.Pending)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    var contribution = await _context.Contribution.Include(c => c.Contributor)
                                       .Include(c => c.Topic)
                                       .FirstOrDefaultAsync(c => c.Id == contributionId);

                    contribution.Status = contributionStatus;

                    _context.Update(contribution);
                    await _context.SaveChangesAsync();

                    var contributorFullname = $"{contribution.Contributor.FirstName} {contribution.Contributor.LastName}";

                    MailboxAddress from = new MailboxAddress("iMarketing system", "*****@*****.**");
                    MailboxAddress to   = new MailboxAddress(contributorFullname, contribution.Contributor.Email);

                    BodyBuilder bodyBuilder = new BodyBuilder();
                    bodyBuilder.TextBody = $"Hello {contributorFullname},\n\n" +
                                           $"Thanks you for your contribution,\n\n" +
                                           $"Best regards,";

                    MimeMessage message = new MimeMessage();
                    message.From.Add(from);
                    message.To.Add(to);
                    message.Subject = $"Contribution for {contribution.Topic.Title} Status";
                    message.Body    = bodyBuilder.ToMessageBody();

                    SmtpClient client = new SmtpClient();
                    client.Connect("smtp.gmail.com", 465, true);
                    client.Authenticate("nguyenthanhphat.a7.2018", "nguyenthanhphat321");

                    client.Send(message);
                    client.Disconnect(true);
                    client.Dispose();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!ContributionExists(contributionId))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
            }

            return(RedirectToAction(nameof(Details), new { id = contributionId }));
        }
Пример #2
0
        public async Task <IActionResult> Mark(int contributionId, ContributionStatus contributionStatus)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    var existContribution = await _context.Contributions.Include(c => c.Contributor).Include(c => c.Submission)
                                            .FirstOrDefaultAsync(c => c.Id == contributionId);


                    existContribution.Status = contributionStatus;

                    _context.Update(existContribution);
                    await _context.SaveChangesAsync();

                    var contributionFullname = $"{existContribution.Contributor.FirstName} {existContribution.Contributor.LastName}";

                    MailboxAddress from = new MailboxAddress("FGW Management System", "*****@*****.**");
                    MailboxAddress to   = new MailboxAddress(contributionFullname, existContribution.Contributor.Email);

                    BodyBuilder bodyBuilder = new BodyBuilder();
                    bodyBuilder.TextBody = $"Hello {contributionFullname}, \n\n" +
                                           $"Your contribution for {existContribution.Submission.Title} is {contributionStatus}\n\n" +
                                           $"Thank you for your contribution, \n\n" +
                                           $"Best regards.";

                    MimeMessage message = new MimeMessage();
                    message.From.Add(from);
                    message.To.Add(to);
                    message.Subject = $"Contribution for {existContribution.Submission.Title} Status";
                    message.Body    = bodyBuilder.ToMessageBody();

                    SmtpClient client = new SmtpClient();

                    client.Connect("smtp.gmail.com", 465, true);
                    client.Authenticate("huynhminhthong1912", "pqdquwmvialvcchv");

                    client.Send(message);
                    client.Disconnect(true);
                    client.Dispose();
                }
                catch
                {
                }
            }

            return(RedirectToAction(nameof(Details), new { id = contributionId }));
        }
Пример #3
0
        public async Task <IActionResult> Upload(Contribution contribution, IFormFile file,
                                                 ContributionStatus contributionStatus, int contributionId, bool[] privacy)
        {
            if (privacy.Length == 3)
            {
                var topic = await _context.Submissions.FirstOrDefaultAsync(t => t.Id == contribution.SubmissionId);

                if (topic.SubmissionDeadline_2 >= DateTime.Now)
                {
                    var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);


                    if (ModelState.IsValid)
                    {
                        var user = await _context.Users.FindAsync(userId);

                        var coordinatorIds = await _context.UserRoles.Where(u => u.RoleId == "Coordinator")
                                             .Select(u => u.UserId)
                                             .ToListAsync();

                        var coordinators = await _context.Users.Where(u => u.DepartmentId == user.DepartmentId &&
                                                                      coordinatorIds.Contains(u.Id)).ToListAsync();

                        var existContribution = await _context.Contributions.FirstOrDefaultAsync(c => c.ContributorId == userId &&
                                                                                                 c.SubmissionId == contribution.SubmissionId);



                        if (existContribution == null)
                        {
                            contribution.Status = ContributionStatus.Pending;

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

                            existContribution = contribution;
                        }

                        else
                        {
                            existContribution.Status = ContributionStatus.Pending;

                            _context.Update(existContribution);
                            await _context.SaveChangesAsync();
                        }

                        if (file.Length > 0)
                        {
                            FileType?fileType;
                            string   fileExtension = Path.GetExtension(file.FileName).ToLower();

                            switch (fileExtension)
                            {
                            case ".doc":
                            case ".docx": fileType = FileType.Document; break;

                            case ".jpg":
                            case ".png": fileType = FileType.Image; break;

                            default: fileType = null; break;
                            }

                            if (fileType != null)
                            {
                                try
                                {
                                    var path = Path.Combine(_Global.PATH_TOPIC, existContribution.SubmissionId.ToString(), user.Number);
                                    if (!Directory.Exists(path))
                                    {
                                        Directory.CreateDirectory(path);
                                    }
                                    // Upload file, create file
                                    var contributionDate = DateTime.Now;
                                    path             = Path.Combine(path, String.Format("{0}.{1:yyyy-MM-dd.ss-mm-HH}{2}", user.Number, contributionDate, fileExtension));
                                    using var stream = new FileStream(path, FileMode.Create);
                                    file.CopyTo(stream);
                                    var newFile = new SubmittedFile();
                                    newFile.ContributionId = existContribution.Id;
                                    newFile.URL            = path;
                                    newFile.Type           = (FileType)fileType;
                                    _context.Add(newFile);
                                    await _context.SaveChangesAsync();

                                    if (coordinators.Count() > 0)
                                    {
                                        foreach (var coordinator in coordinators)
                                        {
                                            var contributionFullname = $"{existContribution.Contributor.FirstName} {existContribution.Contributor.LastName}";
                                            var coordinatorFullName  = $"{coordinator.FirstName} {coordinator.LastName}";

                                            MailboxAddress from = new MailboxAddress("FGW Management System", "*****@*****.**");
                                            MailboxAddress to   = new MailboxAddress(coordinatorFullName, coordinator.Email);

                                            BodyBuilder bodyBuilder = new BodyBuilder();
                                            bodyBuilder.TextBody = $"Hello Coordinator,\n\n" +
                                                                   $"Your student was submited thier contribution with the title is {existContribution.Submission.Title},\n\n" +
                                                                   $"This is contribution by {contributionFullname},\n\n" +
                                                                   $"Please review and give their feedback soon as possible {contributionFullname},\n\n" +
                                                                   $"Thank you for checking this notification,\n\n" +
                                                                   $"Best regards.";

                                            MimeMessage message = new MimeMessage();
                                            message.From.Add(from);
                                            message.To.Add(to);
                                            message.Subject = $"Contribution for {existContribution.Submission.Title} Status";
                                            message.Body    = bodyBuilder.ToMessageBody();

                                            SmtpClient client = new SmtpClient();

                                            client.Connect("smtp.gmail.com", 465, true);
                                            client.Authenticate("huynhminhthong1912", "pqdquwmvialvcchv");

                                            client.Send(message);
                                            client.Disconnect(true);
                                            client.Dispose();
                                        }
                                    }
                                }
                                catch
                                {
                                }
                            }
                        }
                    }
                }
            }
            else
            {
                TempData["PrivacyError"] = "Please accept all our privacy to submit your contribution.";
            }
            return(RedirectToAction(nameof(Details), new { id = contribution.SubmissionId }));
        }