add categories(add, list, list news)

minor form changes
add news controller and views
This commit is contained in:
2024-07-28 18:35:11 +03:30
parent 30ac6724ca
commit a74072b491
22 changed files with 1092 additions and 148 deletions

View File

@ -1,100 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebApplication1.Models;
namespace WebApplication1.Controllers
{
public class AccountController : Controller
{
newswebappEntities db = new newswebappEntities();
// GET: Account
public ActionResult Index()
{
return View();
}
public ActionResult Register()
{
ViewBag.Message = "Register page";
return View();
}
public ActionResult Login()
{
ViewBag.Message = "Login page";
return View();
}
[HttpPost]
public ActionResult Login(Login model)
{
// Replace this with actual authentication logic
if (ModelState.IsValid)
{
var allusers = db.users.ToList();
foreach (var user in allusers)
{
if (model.username == user.usename && model.password == user.password)
{
// Successful login, redirect to home page or desired page
return RedirectToAction("Index", "Home");
}
}
ModelState.AddModelError("", "Invalid username or password");
}
return View(model);
}
[HttpPost]
public ActionResult Register(Register model)
{
if (ModelState.IsValid)
{
// Replace this with actual user creation logic, including password hashing
// and storing user information in a database
/*var allusers = db.users.ToList();
foreach (var user in allusers)
{
if (model.username.ToString() == user.usename.ToString())
{
ModelState.AddModelError("", "username already exists");
return RedirectToAction("Register");
}
}*/
db.users.Add( new user
{
usename = model.username,
password = model.password,
displayname = model.displayname
});
db.SaveChanges();
return RedirectToAction("Home","Index");
}
/*db.users.Add(new user
{
usename = model.username,
password = model.password,
displayname = model.displayname
});
db.SaveChanges();
}*/
// return RedirectToAction("Login");
return View(model);
}
}
}

View File

@ -0,0 +1,141 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using WebApplication1.Models;
namespace WebApplication1.Controllers
{
public class categoriesController : Controller
{
private newswebappEntities db = new newswebappEntities();
// GET: categories
public ActionResult Index()
{
return View(db.categories.ToList());
}
// GET: categories/Details/5
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
category category = db.categories.Find(id);
ViewBag.title = category.title;
if (category == null)
{
return HttpNotFound();
}
return View(db.news.Where(n => n.title.Contains(category.title)));
}
// GET: categories/Create
public ActionResult Create()
{
if (Session["User"] != null)
{
if (Convert.ToInt32(Session["Userrole"]) == 1)
{
return View();
}
else
{
return new HttpStatusCodeResult(HttpStatusCode.Unauthorized);
}
}
else
return RedirectToAction("index","home");
}
// POST: categories/Create
// To protect from overposting attacks, enable the specific properties you want to bind to, for
// more details see https://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "ID,link,title")] category category)
{
if (ModelState.IsValid)
{
category.link = "categories/"+category.title;
db.categories.Add(category);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(category);
}
// GET: categories/Edit/5
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
category category = db.categories.Find(id);
if (category == null)
{
return HttpNotFound();
}
return View(category);
}
// POST: categories/Edit/5
// To protect from overposting attacks, enable the specific properties you want to bind to, for
// more details see https://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "ID,link,title")] category category)
{
if (ModelState.IsValid)
{
db.Entry(category).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(category);
}
// GET: categories/Delete/5
public ActionResult Delete(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
category category = db.categories.Find(id);
if (category == null)
{
return HttpNotFound();
}
return View(category);
}
// POST: categories/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
category category = db.categories.Find(id);
db.categories.Remove(category);
db.SaveChanges();
return RedirectToAction("Index");
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
}
}

View File

@ -0,0 +1,127 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using WebApplication1.Models;
namespace WebApplication1.Controllers
{
public class newsController : Controller
{
private newswebappEntities db = new newswebappEntities();
// GET: news
public ActionResult Index()
{
return View(db.news.ToList());
}
// GET: news/Details/5
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
news news = db.news.Find(id);
if (news == null)
{
return HttpNotFound();
}
return View(news);
}
// GET: news/Create
public ActionResult Create()
{
return View();
}
// POST: news/Create
// To protect from overposting attacks, enable the specific properties you want to bind to, for
// more details see https://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "ID,title,image,link,summary,cat,tag,publishDate,userID,Content,views")] news news)
{
if (ModelState.IsValid)
{
db.news.Add(news);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(news);
}
// GET: news/Edit/5
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
news news = db.news.Find(id);
if (news == null)
{
return HttpNotFound();
}
return View(news);
}
// POST: news/Edit/5
// To protect from overposting attacks, enable the specific properties you want to bind to, for
// more details see https://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "ID,title,image,link,summary,cat,tag,publishDate,userID,Content,views")] news news)
{
if (ModelState.IsValid)
{
db.Entry(news).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(news);
}
// GET: news/Delete/5
public ActionResult Delete(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
news news = db.news.Find(id);
if (news == null)
{
return HttpNotFound();
}
return View(news);
}
// POST: news/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
news news = db.news.Find(id);
db.news.Remove(news);
db.SaveChanges();
return RedirectToAction("Index");
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
}
}

View File

@ -16,7 +16,15 @@ namespace WebApplication1.Controllers
// GET: users
public ActionResult Index()
{
{ if (Convert.ToInt32(Session["Userrole"]) > 0)
{
@ViewBag.role = "admin";
}
else
{
@ViewBag.role = "user";
}
if (Session["User"] != null)
{
if (Session["Userid"] == null)
@ -33,11 +41,18 @@ namespace WebApplication1.Controllers
else
return RedirectToAction("Login");
}// GET: users
public ActionResult allusers()
{
if (Session["User"] != null)
{
return View(db.users.ToList());
if (Convert.ToInt32(Session["Userrole"]) == 1)
{
return View(db.users.ToList());
}
else {
return new HttpStatusCodeResult(HttpStatusCode.Unauthorized);
}
}
else
return RedirectToAction("Login");
@ -56,8 +71,8 @@ namespace WebApplication1.Controllers
// more details see https://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Register([Bind(Include = "ID,usename,password,displayname")] user user)
{
public ActionResult Register([Bind(Include = "ID,usename,password,displayname,role")] user user)
{
if (ModelState.IsValid)
{
@ -79,6 +94,7 @@ namespace WebApplication1.Controllers
//session start
Session["User"] = user.usename;
Session["Userid"] = user.ID;
Session["Userrole"] = user.role;
return RedirectToAction("Index");
}
@ -123,6 +139,7 @@ namespace WebApplication1.Controllers
//session start
Session["User"] = existingUser.usename;
Session["Userid"] = existingUser.ID;
Session["Userrole"] = existingUser.role;
return RedirectToAction("Index");
}
@ -161,7 +178,7 @@ namespace WebApplication1.Controllers
// more details see https://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "ID,usename,password,displayname")] user user)
public ActionResult Edit([Bind(Include = "ID,usename,password,displayname,role")] user user)
{
if (ModelState.IsValid)
{