From f5dabbdcd8137c27cafc19f9cb874179d31fbba4 Mon Sep 17 00:00:00 2001
From: lianpi3
Date: Sun, 4 Aug 2024 17:08:28 +0330
Subject: [PATCH] add view logic
---
Controllers/HomeController.cs | 3 ++-
Controllers/newsController.cs | 43 ++++++++++++++++++++++++----------
Controllers/usersController.cs | 4 +++-
Models/Model1.Context.cs | 1 +
Models/Model1.edmx | 30 ++++++++++++++++++++++++
Models/Model1.edmx.diagram | 1 +
Models/viewlog.cs | 22 +++++++++++++++++
Views/Home/Index.cshtml | 16 ++++++-------
WebApplication1.csproj | 3 +++
9 files changed, 100 insertions(+), 23 deletions(-)
create mode 100644 Models/viewlog.cs
diff --git a/Controllers/HomeController.cs b/Controllers/HomeController.cs
index f4722e0..1bbc9c2 100644
--- a/Controllers/HomeController.cs
+++ b/Controllers/HomeController.cs
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
+using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
@@ -14,7 +15,7 @@ namespace WebApplication1.Controllers
public ActionResult Index()
{
ViewBag.newsCount = db.news.Count();
- ViewBag.views = "";
+ ViewBag.views = db.viewlogs.Where(v => DbFunctions.TruncateTime(v.viewdate) == DateTime.Today).Count();
setTitle();
return View();
}
diff --git a/Controllers/newsController.cs b/Controllers/newsController.cs
index e6cc015..5f83fe0 100644
--- a/Controllers/newsController.cs
+++ b/Controllers/newsController.cs
@@ -15,6 +15,15 @@ namespace WebApplication1.Controllers
public class newsController : Controller
{
private newswebappEntities db = new newswebappEntities();
+ public static string GetUserIpAddress(HttpRequestBase request)
+ {
+ string ipAddress = request.ServerVariables["HTTP_X_FORWARDED_FOR"];
+ if (string.IsNullOrEmpty(ipAddress))
+ {
+ ipAddress = request.ServerVariables["REMOTE_ADDR"];
+ }
+ return ipAddress;
+ }
// GET: news
public ActionResult Index()
@@ -58,21 +67,31 @@ namespace WebApplication1.Controllers
}
var user = db.users.Find(news.userID);
ViewBag.user = user;
- news.views += 1;
- db.SaveChanges();
- /*
- var key = Request.ServerVariables["REMOTE_ADDR"];
- var now = DateTime.UtcNow;
- if (now - View.LastVisited > _timeLimit)
+ // Get the user's IP address
+ string userIpAddress = GetUserIpAddress(Request);
+
+ // Check if this IP address has already viewed this article
+ DateTime thresholdDate = DateTime.Now.AddHours(-24);
+ bool hasViewed = db.viewlogs.Any(v => v.newsid == id && v.ipadd == userIpAddress && v.viewdate >= thresholdDate);
+ if (!hasViewed)
+ {
+ // Increment the view count
+ news.views++;
+ db.SaveChanges();
+
+ // Log the view
+ db.viewlogs.Add(new viewlog
{
- pageView.LastVisited = now;
- _pageViews[key] = pageView;
- return true; // View counted
- }
- return false; // View within time limit, not counted
- */
+ ipadd = userIpAddress,
+ newsid = news.ID,
+ viewdate = DateTime.Now
+ });
+ db.SaveChanges();
+ }
+
return View(news);
+
}
// GET: news/Create
diff --git a/Controllers/usersController.cs b/Controllers/usersController.cs
index f6eb5bf..22646cf 100644
--- a/Controllers/usersController.cs
+++ b/Controllers/usersController.cs
@@ -204,7 +204,9 @@ namespace WebApplication1.Controllers
var hashedBytes = sha256.ComputeHash(Encoding.UTF8.GetBytes(user.password));
user.password = BitConverter.ToString(hashedBytes).Replace("-", "").ToLower();
}
- db.Entry(user).State = EntityState.Modified;
+ var u= db.users.Find(user.ID);
+ u.password = user.password;
+ u.displayname = user.displayname;
db.SaveChanges();
return RedirectToAction("Index");
}
diff --git a/Models/Model1.Context.cs b/Models/Model1.Context.cs
index c4289fc..6181837 100644
--- a/Models/Model1.Context.cs
+++ b/Models/Model1.Context.cs
@@ -31,5 +31,6 @@ namespace WebApplication1.Models
public virtual DbSet tickets { get; set; }
public virtual DbSet users { get; set; }
public virtual DbSet responses { get; set; }
+ public virtual DbSet viewlogs { get; set; }
}
}
diff --git a/Models/Model1.edmx b/Models/Model1.edmx
index 42082d0..77ef1f7 100644
--- a/Models/Model1.edmx
+++ b/Models/Model1.edmx
@@ -69,6 +69,15 @@
+
+
+
+
+
+
+
+
+
@@ -76,6 +85,7 @@
+
@@ -88,6 +98,7 @@
+
@@ -153,6 +164,15 @@
+
+
+
+
+
+
+
+
+
@@ -229,6 +249,16 @@
+
+
+
+
+
+
+
+
+
+
diff --git a/Models/Model1.edmx.diagram b/Models/Model1.edmx.diagram
index 08b62f3..a34b8cd 100644
--- a/Models/Model1.edmx.diagram
+++ b/Models/Model1.edmx.diagram
@@ -11,6 +11,7 @@
+
diff --git a/Models/viewlog.cs b/Models/viewlog.cs
new file mode 100644
index 0000000..6d87067
--- /dev/null
+++ b/Models/viewlog.cs
@@ -0,0 +1,22 @@
+//------------------------------------------------------------------------------
+//
+// This code was generated from a template.
+//
+// Manual changes to this file may cause unexpected behavior in your application.
+// Manual changes to this file will be overwritten if the code is regenerated.
+//
+//------------------------------------------------------------------------------
+
+namespace WebApplication1.Models
+{
+ using System;
+ using System.Collections.Generic;
+
+ public partial class viewlog
+ {
+ public int ID { get; set; }
+ public string ipadd { get; set; }
+ public Nullable newsid { get; set; }
+ public Nullable viewdate { get; set; }
+ }
+}
diff --git a/Views/Home/Index.cshtml b/Views/Home/Index.cshtml
index e26b0d2..fcf2cb8 100644
--- a/Views/Home/Index.cshtml
+++ b/Views/Home/Index.cshtml
@@ -19,15 +19,13 @@
Start Reading »
-
- Get more libraries
- NuGet is a free Visual Studio extension that makes it easy to add, remove, and update libraries and tools in Visual Studio projects.
- Learn more »
-
-
- Web Hosting
- You can easily find a web hosting company that offers the right mix of features and price for your applications.
- Learn more »
+
\ No newline at end of file
diff --git a/WebApplication1.csproj b/WebApplication1.csproj
index 02774ab..99fbb06 100644
--- a/WebApplication1.csproj
+++ b/WebApplication1.csproj
@@ -189,6 +189,9 @@
Model1.tt
+
+ Model1.tt
+