add view logic
This commit is contained in:
@ -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();
|
||||
}
|
||||
|
||||
@ -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)
|
||||
{
|
||||
pageView.LastVisited = now;
|
||||
_pageViews[key] = pageView;
|
||||
return true; // View counted
|
||||
// Increment the view count
|
||||
news.views++;
|
||||
db.SaveChanges();
|
||||
|
||||
// Log the view
|
||||
db.viewlogs.Add(new viewlog
|
||||
{
|
||||
ipadd = userIpAddress,
|
||||
newsid = news.ID,
|
||||
viewdate = DateTime.Now
|
||||
});
|
||||
db.SaveChanges();
|
||||
}
|
||||
return false; // View within time limit, not counted
|
||||
*/
|
||||
|
||||
return View(news);
|
||||
|
||||
}
|
||||
|
||||
// GET: news/Create
|
||||
|
||||
@ -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");
|
||||
}
|
||||
|
||||
@ -31,5 +31,6 @@ namespace WebApplication1.Models
|
||||
public virtual DbSet<ticket> tickets { get; set; }
|
||||
public virtual DbSet<user> users { get; set; }
|
||||
public virtual DbSet<response> responses { get; set; }
|
||||
public virtual DbSet<viewlog> viewlogs { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
@ -69,6 +69,15 @@
|
||||
<Property Name="displayname" Type="nvarchar" MaxLength="50" />
|
||||
<Property Name="role" Type="int" Nullable="false" />
|
||||
</EntityType>
|
||||
<EntityType Name="viewlog">
|
||||
<Key>
|
||||
<PropertyRef Name="ID" />
|
||||
</Key>
|
||||
<Property Name="ID" Type="int" StoreGeneratedPattern="Identity" Nullable="false" />
|
||||
<Property Name="ipadd" Type="nvarchar(max)" />
|
||||
<Property Name="newsid" Type="int" />
|
||||
<Property Name="viewdate" Type="datetime" />
|
||||
</EntityType>
|
||||
<EntityContainer Name="newswebappModelStoreContainer">
|
||||
<EntitySet Name="category" EntityType="Self.category" Schema="dbo" store:Type="Tables" />
|
||||
<EntitySet Name="news" EntityType="Self.news" Schema="dbo" store:Type="Tables" />
|
||||
@ -76,6 +85,7 @@
|
||||
<EntitySet Name="tag" EntityType="Self.tag" Schema="dbo" store:Type="Tables" />
|
||||
<EntitySet Name="ticket" EntityType="Self.ticket" Schema="dbo" store:Type="Tables" />
|
||||
<EntitySet Name="user" EntityType="Self.user" Schema="dbo" store:Type="Tables" />
|
||||
<EntitySet Name="viewlog" EntityType="Self.viewlog" Schema="dbo" store:Type="Tables" />
|
||||
</EntityContainer>
|
||||
</Schema></edmx:StorageModels>
|
||||
<!-- CSDL content -->
|
||||
@ -88,6 +98,7 @@
|
||||
<EntitySet Name="tickets" EntityType="newswebappModel.ticket" />
|
||||
<EntitySet Name="users" EntityType="newswebappModel.user" />
|
||||
<EntitySet Name="responses" EntityType="newswebappModel.response" />
|
||||
<EntitySet Name="viewlogs" EntityType="newswebappModel.viewlog" />
|
||||
</EntityContainer>
|
||||
<EntityType Name="category">
|
||||
<Key>
|
||||
@ -153,6 +164,15 @@
|
||||
<Property Name="adminid" Type="Int32" />
|
||||
<Property Name="ticketid" Type="Int32" />
|
||||
</EntityType>
|
||||
<EntityType Name="viewlog">
|
||||
<Key>
|
||||
<PropertyRef Name="ID" />
|
||||
</Key>
|
||||
<Property Name="ID" Type="Int32" Nullable="false" annotation:StoreGeneratedPattern="Identity" />
|
||||
<Property Name="ipadd" Type="String" MaxLength="Max" FixedLength="false" Unicode="true" />
|
||||
<Property Name="newsid" Type="Int32" />
|
||||
<Property Name="viewdate" Type="DateTime" Precision="3" />
|
||||
</EntityType>
|
||||
</Schema>
|
||||
</edmx:ConceptualModels>
|
||||
<!-- C-S mapping content -->
|
||||
@ -229,6 +249,16 @@
|
||||
</MappingFragment>
|
||||
</EntityTypeMapping>
|
||||
</EntitySetMapping>
|
||||
<EntitySetMapping Name="viewlogs">
|
||||
<EntityTypeMapping TypeName="newswebappModel.viewlog">
|
||||
<MappingFragment StoreEntitySet="viewlog">
|
||||
<ScalarProperty Name="viewdate" ColumnName="viewdate" />
|
||||
<ScalarProperty Name="newsid" ColumnName="newsid" />
|
||||
<ScalarProperty Name="ipadd" ColumnName="ipadd" />
|
||||
<ScalarProperty Name="ID" ColumnName="ID" />
|
||||
</MappingFragment>
|
||||
</EntityTypeMapping>
|
||||
</EntitySetMapping>
|
||||
</EntityContainerMapping>
|
||||
</Mapping>
|
||||
</edmx:Mappings>
|
||||
|
||||
@ -11,6 +11,7 @@
|
||||
<EntityTypeShape EntityType="newswebappModel.ticket" Width="1.5" PointX="0.75" PointY="4.75" />
|
||||
<EntityTypeShape EntityType="newswebappModel.user" Width="1.5" PointX="2.75" PointY="4.75" />
|
||||
<EntityTypeShape EntityType="newswebappModel.response" Width="1.5" PointX="4.75" PointY="4.625" />
|
||||
<EntityTypeShape EntityType="newswebappModel.viewlog" Width="1.5" PointX="3.25" PointY="7.25" />
|
||||
</Diagram>
|
||||
</edmx:Diagrams>
|
||||
</edmx:Designer>
|
||||
|
||||
22
Models/viewlog.cs
Normal file
22
Models/viewlog.cs
Normal file
@ -0,0 +1,22 @@
|
||||
//------------------------------------------------------------------------------
|
||||
// <auto-generated>
|
||||
// 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.
|
||||
// </auto-generated>
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
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<int> newsid { get; set; }
|
||||
public Nullable<System.DateTime> viewdate { get; set; }
|
||||
}
|
||||
}
|
||||
@ -19,15 +19,13 @@
|
||||
<a class="btn btn-outline-dark" href="/news"> Start Reading »</a>
|
||||
</p>
|
||||
</section>
|
||||
<section class="col-md-4" aria-labelledby="librariesTitle">
|
||||
<h2 id="librariesTitle">Get more libraries</h2>
|
||||
<p>NuGet is a free Visual Studio extension that makes it easy to add, remove, and update libraries and tools in Visual Studio projects.</p>
|
||||
<p><a class="btn btn-outline-dark" href="https://go.microsoft.com/fwlink/?LinkId=301866">Learn more »</a></p>
|
||||
</section>
|
||||
<section class="col-md-4" aria-labelledby="hostingTitle">
|
||||
<h2 id="hostingTitle">Web Hosting</h2>
|
||||
<p>You can easily find a web hosting company that offers the right mix of features and price for your applications.</p>
|
||||
<p><a class="btn btn-outline-dark" href="https://go.microsoft.com/fwlink/?LinkId=301867">Learn more »</a></p>
|
||||
<section class="col-md-4" aria-labelledby="viewcount">
|
||||
<h2 id="viewcount">View count</h2>
|
||||
<p>
|
||||
@ViewBag.views
|
||||
</p>
|
||||
<p>
|
||||
<a class="btn btn-outline-dark" href="/news"> Start Reading »</a></p>
|
||||
</section>
|
||||
</div>
|
||||
</main>
|
||||
@ -189,6 +189,9 @@
|
||||
<Compile Include="Models\user.cs">
|
||||
<DependentUpon>Model1.tt</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Include="Models\viewlog.cs">
|
||||
<DependentUpon>Model1.tt</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
|
||||
Reference in New Issue
Block a user