-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProfileController.cs
96 lines (87 loc) · 2.87 KB
/
ProfileController.cs
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
using Preveld.Infrastructure;
using Preveld.Models;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web.Mvc;
namespace Preveld.Controllers
{
public class ProfileController : BaseController
{
private ApplicationDBContext db = new ApplicationDBContext();
[CustomAuthorize]
public ActionResult Index()
{
List<UserProfile> lists = db.UserProfiles.ToList();
return View(lists);
}
[CustomAuthorize]
public ActionResult Create()
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
[CustomAuthorize]
public ActionResult Create(UserProfile user)
{
bool IsUserIDExist = db.UserProfiles.Any(x => x.User_ID == user.User_ID && x.ID != user.ID);
if (IsUserIDExist == true)
{
ModelState.AddModelError("User_ID", "Username already exists");
}
if (ModelState.IsValid)
{
db.UserProfiles.Add(user);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(user);
}
public ActionResult IsUsernameAvailable(string User_ID, int ? ID)
{
try
{
var userProfile = db.UserProfiles.Single(a => a.User_ID == User_ID && a.ID != ID);
return Json(false, JsonRequestBehavior.AllowGet);
}
catch (Exception)
{
return Json(true, JsonRequestBehavior.AllowGet);
}
}
[CustomAuthorize]
public ActionResult Edit(int ID)
{
var userProfile = db.UserProfiles.SingleOrDefault(a => a.ID == ID);
return View(userProfile);
}
[CustomAuthorize]
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(UserProfile userProfile)
{
bool IsUserIDExist = db.UserProfiles.Any(x => x.User_ID == userProfile.User_ID && x.ID != userProfile.ID);
if (IsUserIDExist == true)
{
ModelState.AddModelError("User_ID", "Username already exists");
}
if (ModelState.IsValid)
{
db.Entry(userProfile).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(userProfile);
}
[CustomAuthorize]
public ActionResult Delete(int ID)
{
var userProfile = db.UserProfiles.Find(ID);
db.UserProfiles.Remove(userProfile);
db.SaveChanges();
return RedirectToAction("Index");
}
}
}