Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.04 KB | None | 0 0
  1. using Biblioteka.CustomFilters;
  2. using Biblioteka.DAL;
  3. using Biblioteka.LINQ;
  4. using Biblioteka.Models;
  5. using Biblioteka.ModelView;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Data.Entity;
  9. using System.Linq;
  10. using System.Threading.Tasks;
  11. using System.Web;
  12. using System.Web.Mvc;
  13.  
  14. namespace Biblioteka.Controllers
  15. {
  16. [AdminRole]
  17. public class AdminController : Controller
  18. {
  19. // GET: Admin
  20. public ActionResult Index()
  21. {
  22. return View();
  23. }
  24. [AdminRole]
  25. public ActionResult Admin()
  26. {
  27. DB db = new DB();
  28. if (!db.Longlifes.Any())
  29. {
  30. ViewBag.Longlife = "Długość wypożyczenia: " + db.Longlifes.OrderByDescending(o => o.LonglifeID).FirstOrDefault();
  31. }
  32.  
  33.  
  34.  
  35.  
  36. return View();
  37. }
  38. //ADMIN KONTORLER
  39. [AdminRole]
  40. [HttpPost]
  41. public ActionResult Admin(FormCollection form)
  42. {
  43. string value = Convert.ToString(form["inputName"]);
  44. string lim = Convert.ToString(form["limit"]);
  45. DB db = new DB();
  46. //ViewBag.Longlife = "Długość wypożyczenia: " + db.Longlifes.Last();
  47. if (value != null && value != "")
  48. {
  49. int days = Int32.Parse(value);
  50.  
  51. Longlife l = db.Longlifes.Where(x => x.LonglifeID == 1).FirstOrDefault();
  52. l.longlife = days;
  53. db.Entry(l).State = EntityState.Modified;
  54. db.SaveChanges();
  55. ViewBag.Longlife = "Długość wypożyczenia została ustawiona na: " + days + " dni!";
  56. }
  57. if (lim != null && lim != "")
  58. {
  59. int limit = Int32.Parse(lim);
  60.  
  61. Longlife l = db.Longlifes.Where(x => x.LonglifeID == 1).FirstOrDefault();
  62. l.limit = limit;
  63. db.Entry(l).State = EntityState.Modified;
  64. db.SaveChanges();
  65. ViewBag.Limit = "Limit wypożyczeń został ustawiona na: " + limit + " książek!";
  66. }
  67.  
  68.  
  69. return View();
  70. }
  71.  
  72. public async Task<ActionResult> SendMails()
  73. {
  74. AccountVM acc = new AccountVM();
  75. BorrowingVM bor = new BorrowingVM();
  76. BookVM book = new BookVM();
  77.  
  78. List<Book> books = await Task.Run(() => book.Get_list());
  79. List<Account> accounts = await Task.Run(() => acc.Get_list());
  80. List<Borrowing> borrows = await Task.Run(() => bor.Get_list());
  81.  
  82. var emails = from b in books
  83. from o in borrows
  84. from a in accounts
  85. where o.BookID == b.BookID && a.AccountID == o.ReaderID && o.Returned == false && o.Return_date < DateTime.Now
  86. select new
  87. {
  88. a.Email,
  89. b.Title
  90. };
  91.  
  92. GMailer.GmailUsername = "panmail1212p@gmail.com";
  93. GMailer.GmailPassword = "zaq1@wsx";
  94. GMailer mailer = new GMailer();
  95.  
  96.  
  97. Parallel.ForEach(emails, e =>
  98. {
  99.  
  100. mailer.ToEmail = e.Email;
  101. mailer.Subject = "BIBLIOTEKA - Zwrot książki";
  102. mailer.Body = "Prosimy o zwrot książki '" + e.Title + "' !";
  103. mailer.IsHtml = true;
  104. mailer.Send();
  105. });
  106. ViewBag.Mails1 = "Wysłano próśb " + emails.Count();
  107.  
  108. var emails1 = from b in books
  109. from o in borrows
  110. from a in accounts
  111. where o.BookID == b.BookID && a.AccountID == o.ReaderID && o.Returned == false && o.Return_date.Date == DateTime.Now.Date.AddDays(1)
  112. select new
  113. {
  114. a.Email,
  115. b.Title
  116. };
  117.  
  118. Parallel.ForEach(emails, e =>
  119. {
  120. mailer.ToEmail = e.Email;
  121. mailer.Subject = "BIBLIOTEKA - Przypomnienie o zwrocie książki!";
  122. mailer.Body = "Został jeden dzień do zwrotu książki '" + e.Title + "' ! Po upływie czasu kara zastanie naliczona!";
  123. mailer.IsHtml = true;
  124. mailer.Send();
  125. });
  126.  
  127. ViewBag.Mails2 = "Wysłano przypomnień " + emails1.Count();
  128.  
  129. return View("Admin");
  130. }
  131.  
  132. public ActionResult Activation()
  133. {
  134. AccountVM vm = new AccountVM();
  135. //var list = (from i in vm.Get_list()
  136. // where i.Active==false
  137. // select new Activa( i.AccountID, i.Name, i.Surname, i.Active));
  138. var list =vm.Get_list().Where(x=>x.Active==false);
  139.  
  140.  
  141. ViewBag.ac = list.ToList();
  142.  
  143. return View(list.ToList());
  144. }
  145.  
  146. }
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement