Online Mağaza Projesi Bölüm1
Herkese selamlar;
Bu uygulamada amaç bir kuyumcu mağzası için, üyelik gerektiren ve loglama yapılan bir websitesi tasarlamaktır. Çeşitli ürünlerin admin ekranından girilmesi ve son kullanıcın bunu anında görmesi için signalR teknolojisi kullanılacaktır.Database Sql ve CodeFirst olacaktır. Bu sayede önceki makalelerimde bahsettiğim teknolojilerin biraz olsun üstünden geçilecek aynı zamanda gerçek hayatta karşınıza çıkabilecek sorunlara ışık tutulacaktır.
1 2 3 4 5 6 7 |
public ActionResult Index() { if (Session["UserID"] == null) { return RedirectToAction("Login"); } } |
Öncelikle üyelik ve login konusunu konuşalım. User siteye gelidiği zaman doğal olarak ilk etapta Index view’a yönlendirilir. Yukarıda görüldüğü üzere Session[“UserID”] kontrolü ile kullanıcının siteye önceden giriş yapıp yapılmadığına bakılır ve değer null ise Login Action’ına yönlendirilir.
Üyelik ekranında yukarıda görüldüğü gibi beni hatırla seçeneği vardır. İlgili cookie’ye bakıldıktan sonra eğer değer var ise email kutucuğu doldurulur. Sign in butonuna basılınca aşağıda görüldüğü gibi saveUser() function’ı çağrılır. Girilen email ve password’e göre aşağıdaki LoginState Enum ile kategorize edilmiş durumlar ortaya çıkar..
1 2 3 4 5 6 7 8 |
public enum LoginState { NoRecord=1, Recorded=2, WaitForApprove=3, WaitForApproveWrongPassword = 4, WrongPassword = 5 } |
1-)Hiç kayıt yok ise SignIn ekranına, girilen değerler ile birlikta aşağıda görüldüğü gibi yönlendirilir.
1 2 3 4 5 6 7 |
if (isOK == 1) { var url = '@Url.Action("SignIn", new { Password = "__code__", Email = "__code2__" })'; url = url.replace('__code2__', $("#Email").val()); url = url.replace('__code__', $("#Password").val()); url = url.replace("&", "").replace(";", "&"); window.location.href = url; } |
2-)Kayıtlı ve onaylanmış user Index Ekranına yönlendirilir.
1 2 3 |
else if (isOK == 2) { window.location.href = "/Home/Index"; } |
3-)Aşağıda görüldüğü gibi girilen kayıda göre user var ama belirtilen email’le gönderilen onay mailine tıklanmamış, kısaca mail onaylanmamış ise tekrardan gönderilmesinin istenip istenmediği sorulur ve cevaba göre tekrardan onay maili gönderilir.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
else if (isOK == 3) { var result = confirm("Mail onaylanmamıştır. Tekrar onay maili atılsın mı?"); if (result == true) { $.post("/Home/SendConfirmMail", { 'email': $("#Email").val() }, function (isSend) { if (isSend) { alert("We Sent to Confirmation Mail.Please Go to Your Mail and Click The Url!"); window.location.href = "/Home/Index"; } else { alert("There is an error while processing!"); } }); } } |
Aşağıda görüldüğü gibi Onay Maili gönderme işleminde SMTP yerine bilerek gmail kullanıldı. Çünkü evde veya şirket dışında smtp bulmak her zaman çok kolay olmayabilir. Gmail’den 587 portundan account’unuzda gerekli izinleri verdikten sonra kolayca mail atabilirsiniz. İlgili izin ekranı ve kodu aşağıdadır.
Aşağıdaki kodda görüldüğü gibi onay maili atılmadan önce bir guid number üretilmektedir. Bu guid maildeki link içine gömülmektedir. Bu link onay amaçlı tıklanınca ConfirmJewelleryLogin Action’ına gidilecek ve aranan user bu guid’den bulunarak onaylanacaktır.
Home/SendConfirmMail:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
[HttpPost] public bool SendConfirmMail(string email) { try { string Guid = string.Empty; using (JewelleryStoreDB dbContext = new JewelleryStoreDB()) { Guid = dbContext.tblUser.FirstOrDefault(us => us.Email == email).GuidID.ToString(); } SmtpClient sc = new SmtpClient(); sc.Port = 587; sc.Host = "smtp.gmail.com"; sc.EnableSsl = true; sc.Credentials = new NetworkCredential("Your Mail", "Your Password"); MailMessage mail = new MailMessage(); mail.From = new MailAddress("JewelleryStore@gmail.com", "Jewellery Store"); mail.To.Add(email); mail.Subject = "JewelleryStore Login Confirm Link"; mail.IsBodyHtml = true; mail.Body = "<a href='http://localhost:17716/ConfirmJewelleryLogin/" + Guid + "' TARGET = '_blank'>Please Confirm Your Mail!</a>"; TARGET = '_blank'>Please Confirm Your Mail!</a>"; sc.Send(mail); return true; } catch (Exception ex) { return false; } }2 |
ConfirmJewelleryLogin Action: Maildeki link tıklanınca gelinen actiondır.Amacı onaylanacak user’ı urlden gelen guid ile bularak onaylamaktır.
1 2 3 4 5 6 7 8 9 10 11 12 |
public ActionResult ConfirmJewelleryLogin(Guid guid) { using (JewelleryStoreDB dbContext = new JewelleryStoreDB()) { var user = dbContext.tblUser.First(us => us.GuidID == guid); user.isChecked = true; dbContext.SaveChanges(); Session["UserID"] = user.ID; } return RedirectToAction("Admin"); } |
4 ve 5-) Bu durumlarda password yanlış girilmiştir. 4. maddede hem password yanlıştır hem de üyelik onay beklemektedir. 5. durumda sadece password yanlıştır.Bu durumlarda password’ün hatalı olduğunu göstermek amaçlı kırmızı ile çevrelenir. İlgili kod aşağıdadır.
1 2 3 |
else if (isOK == 4 || isOK == 5) { $("#Password").css('border-color', '#FF0000'); } |
Login.cshtml:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
@{ ViewBag.Title = "Login"; } <head> <!-- Bootstrap core CSS --> <link href="/Content/bootstrap.min.css" rel="stylesheet"> <!-- Custom styles for this template --> <link href="/Content/signin.css" rel="stylesheet"> <!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries --> <!--[if lt IE 9]> <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script> <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script> <![endif]--> <script src="~/Scripts/JewelleryScript.js"></script> <script type="text/javascript"> $(document).ready(function () { $('#Email').bind('keypress', function (e) { var code = e.keyCode || e.which; if (code == 13) { $('#Password').focus(); } }); $('#Password').bind('keypress', function (e) { var code = e.keyCode || e.which; if (code == 13) { saveUser(); } }); }); function saveUser() { if (validateEmail($("#Email").val())) { $("#Email").css('border-color', ''); $.post("/Home/Login", { 'Email': $("#Email").val(), 'Password': $("#Password").val(), 'IsRemmber': $("#isRemmember").is(':checked') }, function (isOK) { if (isOK == 1) { var url = '@Url.Action("SignIn", new { Password = "__code__", Email = "__code2__" })'; url = url.replace('__code2__', $("#Email").val()); url = url.replace('__code__', $("#Password").val()); url = url.replace("&", "").replace(";", "&"); window.location.href = url; } else if (isOK == 2) { window.location.href = "/Home/Index"; } else if (isOK == 3) { var result = confirm("Mail onaylanmamıştır. Tekrar onay maili atılsın mı?"); if (result == true) { $.post("/Home/SendConfirmMail", { 'email': $("#Email").val() }, function (isSend) { if (isSend) { alert("We Sent to Confirmation Mail.Please Go to Your Mail and Click The Url!"); window.location.href = "/Home/Index"; } else { alert("There is an error while processing!"); } }); } } else if (isOK == 4 || isOK == 5) { $("#Password").css('border-color', '#FF0000'); } }); } else { $("#Email").css('border-color', '#FF0000'); $("#Email").focus(); } }; </script> </head> <body> <div class="container"> <form class="form-signin" role="form"> <h2 class="form-signin-heading">Please sign in</h2> <input type="email" class="form-control" placeholder="Email address" required autofocus id="Email" value="@ViewBag.Email"> <input type="password" class="form-control" placeholder="Password" required id="Password"> <div class="checkbox"> <label> <input type="checkbox" value="remember-me" id="isRemmember"> Remember me </label> <label>    <a href="/Home/SignIn"> Sign In New User </a> </label> </div> <button class="btn btn-lg btn-primary btn-block" type="button" onclick="saveUser();">Sign in</button> </form> </div> </body> |
HomeController/Login Action:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
public int Login(User user) { //Cookie İşlemleri if (user.IsRemmber) { HttpCookie userCookie = new HttpCookie("IsRemmber"); userCookie.Value = user.Email; userCookie.Expires = DateTime.Now.AddDays(1d); Response.Cookies.Add(userCookie); } //Bitti-------------- using (JewelleryStoreDB dbContext = new JewelleryStoreDB()) { var usr = dbContext.tblUser.FirstOrDefault(us => us.Email == user.Email && us.Password == user.Password); if (usr == null || usr.isChecked == false) { if (usr != null && usr.isChecked == false) { return (int)LoginState.WaitForApprove;//Kayıt var Onaylanmamış } else if (dbContext.tblUser.FirstOrDefault(us => us.Email == user.Email) != null) { if (dbContext.tblUser.FirstOrDefault(us => us.Email == user.Email).isChecked == false) { return (int)LoginState.WaitForApproveWrongPassword;//Şifre Yanlış Onaylanmamış Mail; } return (int)LoginState.WrongPassword;//Şifre yanlış } else { return (int)LoginState.NoRecord;//Kayıt yok } } else { Session["UserID"] = usr.ID; } } return (int)LoginState.Recorded;//Kayıtlı } |
Şimdi yeni bir kullanıcı kaydının yapıldığı SignIn View’ına bakalım:
Aşağıda görüldüğü gibi Name,Surname Ve Email’de enter’a basılınca bir sonraki textbox’a gidilmektedir. Password’de enter’a basılınca saveUser() function’ı ile jquery post sayesinde SignIn Action’ına gidilir ve dönen değer 1 ise onay maili gönderildi mesajı verilir. Dönen değer 2 ise bir hata var demektir, eğer 3 ise bu mail zaten kullanılıyor demektir.
Views/Home/SignIn:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
@model DAL.tblUser @{ ViewBag.Title = "SignIn"; } <head> <!-- Bootstrap core CSS --> <link href="/Content/bootstrap.min.css" rel="stylesheet"> <!-- Custom styles for this template --> <link href="/Content/signin.css" rel="stylesheet"> <script src="~/Scripts/JewelleryScript.js"></script> <script type="text/javascript"> $(document).ready(function () { $('#Name').bind('keypress', function (e) { var code = e.keyCode || e.which; if (code == 13) { $('#Surname').focus(); } }); $('#Surname').bind('keypress', function (e) { var code = e.keyCode || e.which; if (code == 13) { $('#Email').focus(); } }); $('#Email').bind('keypress', function (e) { var code = e.keyCode || e.which; if (code == 13) { $('#Password').focus(); } }); $('#Password').bind('keypress', function (e) { var code = e.keyCode || e.which; if (code == 13) { saveUser(); } }); }); function saveUser() { if (validateEmail($("#Email").val())) { $("#Email").css('border-color', ''); $.post("/Home/SignIn", { 'Email': $("#Email").val(), 'Password': $("#Password").val(), 'Name': $("#Name").val(), 'Surname': $("#Surname").val() }, function (isOK) { if (isOK==1) { alert("We Sent to Confirmation Mail.Please Go to Your Mail and Click The Url!"); window.location.href = "/Home/Index"; } else if(isOK==2) { alert("There is an error while processing!"); } else if (isOK == 3) { alert("Ther is a same Mail as Recorded!"); $("#Email").css('border-color', '#FF0000'); $("#Email").focus(); } }); } else { $("#Email").css('border-color', '#FF0000'); $("#Email").focus(); } }; </script> </head> <br /> <div class="container"> <table align="center"> <tr><td><h2>New User</h2></td></tr> <tr> <td> Name: </td> <td> <input class="form-control" type="text" id="Name" autofocus style="width:200px" /> </td> </tr> <tr> <td> Surname: </td> <td> <input class="form-control" type="text" id="Surname" style="width:200px" /> </td> </tr> <tr> <td> Email: </td> <td> <input class="form-control" type="email" id="Email" value="@Model.Email" style="width:200px" /> </td> </tr> <tr> <td> Password: </td> <td> <input class="form-control" type="password" id="Password" value="@Model.Password" style="width:200px" /> </td> </tr> <tr> <td> <input class="btn btn-lg btn-primary btn-block" type="button" onclick="saveUser();" value="Record" /> </td> </tr> </table> </div> |
Aşağıda görüldüğü gibi SignIn Action’ında yeni user için Guid üretilir. Bu uniq string değeri onay mail url’i içine gömülür ve gönderilir. Daha sonra bu email’e ait kayıtlı bir user varmı diye bakılıp ilgili mesaj gönderilir.
HomeController/SignIn:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
[HttpPost] [LogAttribute(LogType.WaitForConfirm)] public int SignIn(tblUser user) { try { Guid _Guid = System.Guid.NewGuid(); user.GuidID = _Guid; user.isChecked = false; SendConfirmMail(_Guid.ToString(), user.Email); using (JewelleryStoreDB dbContext = new JewelleryStoreDB()) { if (dbContext.tblUser.FirstOrDefault(us => us.Email == user.Email) != null) { return 3; } dbContext.tblUser.Add(user); dbContext.SaveChanges(); } return 1; } catch (Exception) { return 2; } } |
Biraz da Database’i konuşalım. Projede CodeFirst kullanılmıştır. İlgili tablo şemaları ve dependency aşağıda görüldüğü gibidir.
DataModel ve DataContext kodları ayrı bir proje olan DAL altında aşağıdaki gibidir.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
namespace DAL { using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; using System.Data.Entity.Spatial; [Table("tblLog")] public partial class tblLog { public int ID { get; set; } public int? UserID { get; set; } [StringLength(50)] public string LogType { get; set; } public int? ProductID { get; set; } public DateTime? CreatedDate { get; set; } public virtual tblUser tblUser { get; set; } } } namespace DAL { using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; using System.Data.Entity.Spatial; [Table("tblProduct")] public partial class tblProduct { public int ID { get; set; } [StringLength(50)] public string Name { get; set; } [StringLength(100)] public string ImageUrl { get; set; } [Column(TypeName = "money")] public decimal? Price { get; set; } [StringLength(200)] public string Description { get; set; } public DateTime? CreatedDate { get; set; } } } namespace DAL { using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; using System.Data.Entity.Spatial; [Table("tblUser")] public partial class tblUser { public tblUser() { tblLog = new HashSet<tblLog>(); } public int ID { get; set; } [StringLength(50)] public string Name { get; set; } [StringLength(50)] public string Surname { get; set; } [StringLength(50)] public string Password { get; set; } [StringLength(50)] public string Email { get; set; } public bool? isChecked { get; set; } public Guid? GuidID { get; set; } public virtual ICollection<tblLog> tblLog { get; set; } } } namespace DAL { using System; using System.Data.Entity; using System.ComponentModel.DataAnnotations.Schema; using System.Linq; public partial class JewelleryStoreDB : DbContext { public JewelleryStoreDB() : base("name=JewelleryStoreDB") { } public virtual DbSet<tblLog> tblLog { get; set; } public virtual DbSet<tblProduct> tblProduct { get; set; } public virtual DbSet<tblUser> tblUser { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity<tblLog>() .Property(e => e.LogType) .IsUnicode(false); modelBuilder.Entity<tblProduct>() .Property(e => e.Name) .IsUnicode(false); modelBuilder.Entity<tblProduct>() .Property(e => e.ImageUrl) .IsUnicode(false); modelBuilder.Entity<tblProduct>() .Property(e => e.Price) .HasPrecision(19, 4); modelBuilder.Entity<tblProduct>() .Property(e => e.Description) .IsUnicode(false); modelBuilder.Entity<tblUser>() .Property(e => e.Name) .IsUnicode(false); modelBuilder.Entity<tblUser>() .Property(e => e.Surname) .IsUnicode(false); modelBuilder.Entity<tblUser>() .Property(e => e.Password) .IsUnicode(false); modelBuilder.Entity<tblUser>() .Property(e => e.Email) .IsUnicode(false); modelBuilder.Entity<tblUser>() .HasMany(e => e.tblLog) .WithOptional(e => e.tblUser) .HasForeignKey(e => e.UserID); } } } |
Database’deki dependency aşağıda görüldüğü gibidir. Product tablosunun Log ile relation’ının olmamasını nedeni silinmesinden dolayıdır. Bunun üzerine ilerde admin kısmında daha deytaylı konuşacağız.
Tablolarda da görüldüğü üzere loglama işlemi yapılmaktadır. Şimdi loglama kısmını inceleyelim: Loglama işlemini çok genel düşünmek gerekir. Birçok yerde kullanılacağı için kod kalabalığından mümkün olduğunca kaçınılmalıdır. Ayrıca kolay kullanım ve anlaşılabilirlik esas hedef olmalıdır. Öncelikle aşağıda görülen enum LogType’larımız belirlenir. Bunlar logları tutulacak durumlardır.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
public enum LogType { SignIn = 1, LogIn = 2, WaitForConfirm=3, LogOut=4, ProductDelete=5, ProductInsert = 6, ProductUpdate = 7, Admin = 8, Log=9, Home=10 } |
En kolay kullanım şekli olarak düşünülen ActionFilterAttribut‘dan türemiş class, loglama yapılacak ActionIn başına log tipi ve önceliği belirtilerek aşağıda görüldüğü gibi konulmuştur:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
[LogAttribute(LogType.Home,false)] public ActionResult Index() -------------------------------------- [HttpPost] [LogAttribute(LogType.LogIn,false)] public int Login(User user) -------------------------------------- [HttpPost] [LogAttribute(LogType.WaitForConfirm,false)] public int SignIn(tblUser user) -------------------------------------- [LogAttribute(LogType.SignIn,false)] public ActionResult ConfirmJewelleryLogin(Guid guid) -------------------------------------- [LogAttribute(LogType.LogOut,true)] public ActionResult LogOut() -------------------------------------- [LogAttribute(LogType.Admin,false)] public ActionResult Admin() -------------------------------------- [HttpPost] [LogAttribute(LogType.ProductInsert,false)] public int Admin(tblProduct product) -------------------------------------- [LogAttribute(LogType.ProductUpdate,false)] public int AdminUpdate(tblProduct product) -------------------------------------- [HttpPost] [LogAttribute(LogType.ProductDelete,false)] public PartialViewResult ProductDelete(int DetailID) -------------------------------------- [LogAttribute(LogType.Log,true)] public ActionResult Log() |
Aşağıda ActionFilterAttribute’den türemiş Model/LogAttribute class’ı görülmektedir: İki methodu vardır.
- OnActionExecuted
- OnActionExecuting
OnActionExecuted: Bu method’da Action tamamlandıktan sonra çağrıldığı için; ürün girişi, silme ve güncelleme işlemleri bu method altında loglanır. İşlemin sonunda loglama yapılır ki yeni girilen ProductID kaydedilebilsin.
OnActionExecuting:Bu method’da Action’a başlanamdan önce loglama yapılacağı için LogOut ve Log işlemlerinde bu method altında loglama yapılmıştır. Amaç Log ekranına girildiğinde enson log işlemi yapıldığını göstermektir.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
using DAL; using JewelleryStore.Models; using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace JewelleryStore.Models { [AttributeUsageAttribute(AttributeTargets.All, AllowMultiple = true)] public class LogAttribute : ActionFilterAttribute { public LogAttribute(LogType type,bool isBefore) { LgType = type; IsBefore = isBefore; } public LogType LgType { get; set; } public bool IsBefore { get; set; } public override void OnActionExecuted(ActionExecutedContext filterContext) { if (filterContext.HttpContext.Session["UserID"] != null && IsBefore==false) { if (filterContext.HttpContext.Session["ProductID"] != null) { using (JewelleryStoreDB dbContext = new JewelleryStoreDB()) { tblLog log = new tblLog(); log.CreatedDate = DateTime.Now; log.LogType = LgType.ToString(); log.UserID = int.Parse(filterContext.HttpContext.Session["UserID"].ToString()); log.ProductID = int.Parse(filterContext.HttpContext.Session["ProductID"].ToString()); dbContext.tblLog.Add(log); dbContext.SaveChanges(); if (filterContext.HttpContext.Session["ProductID"] != null) { filterContext.HttpContext.Session.Remove("ProductID"); } } } else { using (JewelleryStoreDB dbContext = new JewelleryStoreDB()) { tblLog log = new tblLog(); log.CreatedDate = DateTime.Now; log.LogType = LgType.ToString(); log.UserID = int.Parse(filterContext.HttpContext.Session["UserID"].ToString()); dbContext.tblLog.Add(log); dbContext.SaveChanges(); } } } } public override void OnActionExecuting(ActionExecutingContext filterContext) { if (filterContext.HttpContext.Session["UserID"] != null && IsBefore==true) { using (JewelleryStoreDB dbContext = new JewelleryStoreDB()) { tblLog log = new tblLog(); log.CreatedDate = DateTime.Now; log.LogType = LgType.ToString(); log.UserID = int.Parse(filterContext.HttpContext.Session["UserID"].ToString()); dbContext.tblLog.Add(log); dbContext.SaveChanges(); } } } } } |
Log Ekranı aşağıda görüldüğü gibidir.
HomeControl/Log Action :
Yukardaki Log ekranın’ı dolduran action’ın kodları aşağıdadır. Görüldüğü üzere Action başlamadan önce log tablosuna kayıt atılmaktadır. [LogAttribute(LogType.Log,true)]
Böylece log ekranında en son görülen kayıt o an girilen log ekranıdır. İlgili log dataları ensondan başa doğru ilgili view model’e doldurulur.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
[LogAttribute(LogType.Log,true)] public ActionResult Log() { if (Session["UserID"] == null) { return RedirectToAction("Login"); } ViewBag.Title = "Log"; List<DAL.tblLog> data; List<Log> LogData = new List<Log>(); using (JewelleryStoreDB dbContext = new JewelleryStoreDB()) { data = dbContext.tblLog.OrderByDescending(tg=>tg.ID).ToList(); foreach (var item in data) { Log lg = new Log(); lg.ID = item.ID; lg.LogType = item.LogType; var product = dbContext.tblProduct.FirstOrDefault(tp => tp.ID == item.ProductID); var user = dbContext.tblUser.FirstOrDefault(us => us.ID == item.UserID); string prdoct=item.ProductID==null?"-":item.ProductID.ToString(); lg.ProductName = product == null ? prdoct : product.Name; lg.UserName = user == null ? item.UserID.ToString() : user.Name; lg.CreatedDate = item.CreatedDate; LogData.Add(lg); } } return View(LogData); } |
Buraya kadar olan kısımda üyelik, gmail üzerinden onay maili, custom action filter ile loglama kısımlarına değindik. Bundan sonra admin ekranında ürün girişi, düzenlenmesi, silinmesi ve bu işlemler yapıldıktan sonra oluşan değişikliklerin client tarafına yansıtılması yani bu işlem için kullanılan signalR üzerine konuşacağız.
Geldik bir makalenin daha sonuna yeni bir makalede görüşmek üzere.
Örnek Url(İptal): http://jewellerystores.azurewebsites.net/
Son Yorumlar