Browse Source

Fixed multiple exceptions.

master
Teknikode 7 years ago
parent
commit
fcf8c9bc1a
  1. 2
      Teknik/App_Start/BundleConfig.cs
  2. 3
      Teknik/Areas/Blog/Controllers/BlogController.cs
  3. 2
      Teknik/Areas/Blog/Views/Blog/Blog.cshtml
  4. 2
      Teknik/Areas/Error/Controllers/ErrorController.cs
  5. 3
      Teknik/Areas/Error/Views/Error/Exception.cshtml
  6. 1
      Teknik/Areas/Error/Views/Error/General.cshtml
  7. 4
      Teknik/Areas/Error/Views/Error/Http404.cshtml
  8. 5
      Teknik/Areas/Error/Views/Error/Http500.cshtml
  9. 39
      Teknik/Areas/Profile/Controllers/ProfileController.cs
  10. 2
      Teknik/Areas/Profile/Scripts/Profile.js
  11. 9
      Teknik/Areas/Profile/Views/Profile/Index.cshtml
  12. 1
      Teknik/Areas/Profile/Views/Profile/Login.cshtml
  13. 1
      Teknik/Areas/Profile/Views/Profile/Register.cshtml
  14. 11
      Teknik/Global.asax.cs
  15. 9
      Teknik/ViewModels/ViewModelBase.cs

2
Teknik/App_Start/BundleConfig.cs

@ -18,9 +18,9 @@ namespace Teknik @@ -18,9 +18,9 @@ namespace Teknik
bundles.Add(new ScriptBundle("~/bundles/common").Include(
"~/Scripts/jquery-{version}.js",
"~/Scripts/jquery.validate*",
"~/Scripts/common.js",
"~/Scripts/bootstrap.js",
"~/Scripts/bootstrap-select.js",
"~/Scripts/common.js",
"~/Scripts/respond.js"));
// Use the development version of Modernizr to develop with and learn from. Then, when you're

3
Teknik/Areas/Blog/Controllers/BlogController.cs

@ -24,7 +24,7 @@ namespace Teknik.Areas.Blog.Controllers @@ -24,7 +24,7 @@ namespace Teknik.Areas.Blog.Controllers
public ActionResult Blog(string username)
{
Models.Blog blog = null;
BlogViewModel model = null;
BlogViewModel model = new BlogViewModel();
// The blog is the main site's blog
if (string.IsNullOrEmpty(username))
{
@ -62,6 +62,7 @@ namespace Teknik.Areas.Blog.Controllers @@ -62,6 +62,7 @@ namespace Teknik.Areas.Blog.Controllers
return View(model);
}
model.Error = true;
return View(model);
}

2
Teknik/Areas/Blog/Views/Blog/Blog.cshtml

@ -26,7 +26,7 @@ @@ -26,7 +26,7 @@
@Scripts.Render("~/bundles/blog")
<div class="container">
@if (Model != null && Model.User != null)
@if (!Model.Error)
{
<div class="row">
<div class="col-sm-12 blog-heading">

2
Teknik/Areas/Error/Controllers/ErrorController.cs

@ -42,7 +42,7 @@ namespace Teknik.Areas.Error.Controllers @@ -42,7 +42,7 @@ namespace Teknik.Areas.Error.Controllers
{
ViewBag.Title = "404 - " + Config.Title;
ViewBag.Message = "Uh Oh, can't find it!";
Response.StatusCode = 404;
ErrorViewModel model = new ErrorViewModel();

3
Teknik/Areas/Error/Views/Error/Exception.cshtml

@ -8,7 +8,8 @@ @@ -8,7 +8,8 @@
<div class="error-details">
An unexpected error has occurred. Please contact the system administrator.
<br />
@if (Model != null && HttpContext.Current.IsDebuggingEnabled)
<br />
@if (Model != null && Model.Exception != null)
{
<div class="text-left">
<p>

1
Teknik/Areas/Error/Views/Error/General.cshtml

@ -9,6 +9,7 @@ @@ -9,6 +9,7 @@
<div class="error-details">
Sorry, an error has occured: @Model.Description
</div>
<br />
<div class="error-actions">
<a href="@Url.SubRouteUrl("www", "Home.Index")" class="btn btn-primary btn-lg">
<span class="glyphicon glyphicon-home"></span>

4
Teknik/Areas/Error/Views/Error/Http404.cshtml

@ -4,10 +4,10 @@ @@ -4,10 +4,10 @@
<div class="row">
<div class="col-md-12">
<div class="error-template text-center">
<h1>Oops!</h1>
<h1>Uh Oh!</h1>
<h2>404 Not Found</h2>
<div class="error-details">
Sorry, an error has occured, Requested page not found!
Unable to find the resource you are looking for. Please contact an Administrator if you think this is in error.
</div>
<br />
<div class="error-actions">

5
Teknik/Areas/Error/Views/Error/Http500.cshtml

@ -4,12 +4,13 @@ @@ -4,12 +4,13 @@
<div class="row">
<div class="col-md-12">
<div class="error-template text-center">
<h1>Oops!</h1>
<h1>Whoops!</h1>
<h2>500 Server Error</h2>
<div class="error-details">
Sorry, a server error occured. Please contact an Administrator about this error.
<br />
@if (Model != null && HttpContext.Current.IsDebuggingEnabled)
<br />
@if (Model != null && Model.Exception != null)
{
<div class="text-left">
<p>

39
Teknik/Areas/Profile/Controllers/ProfileController.cs

@ -6,6 +6,8 @@ using System.Web; @@ -6,6 +6,8 @@ using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using Teknik.Areas.Blog.Models;
using Teknik.Areas.Error.Controllers;
using Teknik.Areas.Error.ViewModels;
using Teknik.Areas.Profile.Models;
using Teknik.Areas.Profile.ViewModels;
using Teknik.Controllers;
@ -28,18 +30,18 @@ namespace Teknik.Areas.Profile.Controllers @@ -28,18 +30,18 @@ namespace Teknik.Areas.Profile.Controllers
username = User.Identity.Name;
}
ProfileViewModel model = null;
ProfileViewModel model = new ProfileViewModel();
ViewBag.Title = "User Does Not Exist - " + Config.Title;
ViewBag.Message = "The User does not exist";
User user = db.Users.Where(u => u.Username == username).First();
var userQuery = db.Users.Where(u => u.Username == username);
if (user != null)
if (userQuery != null && userQuery.Any())
{
Models.User user = userQuery.First();
ViewBag.Title = username + "'s Profile - " + Config.Title;
ViewBag.Message = "Viewing " + username + "'s Profile";
model = new ProfileViewModel();
model.UserID = user.UserId;
model.Username = user.Username;
model.Email = string.Format("{0}@{1}", user.Username, Config.Host);
@ -50,13 +52,16 @@ namespace Teknik.Areas.Profile.Controllers @@ -50,13 +52,16 @@ namespace Teknik.Areas.Profile.Controllers
model.Quote = user.Quote;
// fill in Blog details
Blog.Models.Blog blog = db.Blogs.Where(b => b.UserId == user.UserId && b.BlogId != Constants.SERVERBLOGID).First();
if (blog != null)
var blog = db.Blogs.Where(b => b.UserId == user.UserId && b.BlogId != Constants.SERVERBLOGID);
if (blog != null && blog.Any())
{
model.BlogTitle = blog.Title;
model.BlogDescription = blog.Description;
Blog.Models.Blog foundBlog = blog.First();
model.BlogTitle = foundBlog.Title;
model.BlogDescription = foundBlog.Description;
}
return View(model);
}
model.Error = true;
return View(model);
}
@ -143,6 +148,7 @@ namespace Teknik.Areas.Profile.Controllers @@ -143,6 +148,7 @@ namespace Teknik.Areas.Profile.Controllers
// Generate blog for the user
var newBlog = db.Blogs.Create();
newBlog.UserId = db.Users.Where(u => u.Username == model.Username).Select(u => u.UserId).First();
db.Blogs.Add(newBlog);
db.SaveChanges();
}
catch (Exception ex)
@ -163,9 +169,10 @@ namespace Teknik.Areas.Profile.Controllers @@ -163,9 +169,10 @@ namespace Teknik.Areas.Profile.Controllers
User user = db.Users.Where(u => u.Username == User.Identity.Name).First();
if (user != null)
{
Blog.Models.Blog blog = db.Blogs.Where(b => b.UserId == user.UserId && b.BlogId != Constants.SERVERBLOGID).First();
if (blog != null)
var foundBlog = db.Blogs.Where(b => b.UserId == user.UserId && b.BlogId != Constants.SERVERBLOGID);
if (foundBlog != null && foundBlog.Any())
{
Blog.Models.Blog blog = foundBlog.First();
// Changing Password?
if (!string.IsNullOrEmpty(curPass) && (!string.IsNullOrEmpty(newPass) || !string.IsNullOrEmpty(newPassConfirm)))
{
@ -204,12 +211,14 @@ namespace Teknik.Areas.Profile.Controllers @@ -204,12 +211,14 @@ namespace Teknik.Areas.Profile.Controllers
{
if (ModelState.IsValid)
{
User user = db.Users.Where(u => u.Username == User.Identity.Name).First();
if (user != null)
var user = db.Users.Where(u => u.Username == User.Identity.Name);
if (user != null && user.Any())
{
db.Users.Remove(user);
Models.User foundUser = user.First();
db.Users.Remove(foundUser);
db.SaveChanges();
return Logout();
FormsAuthentication.SignOut();
return Json(new { result = true });
}
}
return Json(new { error = "Unable to delete user." });

2
Teknik/Areas/Profile/Scripts/Profile.js

@ -8,7 +8,7 @@ @@ -8,7 +8,7 @@
data: AddAntiForgeryToken({}),
success: function (html) {
if (html.result) {
window.location.reload();
window.location.replace(homeUrl);
}
else {
$("#top_msg").css('display', 'inline', 'important');

9
Teknik/Areas/Profile/Views/Profile/Index.cshtml

@ -1,19 +1,18 @@ @@ -1,19 +1,18 @@
@model Teknik.Areas.Profile.ViewModels.ProfileViewModel
<script>
var homeUrl = '@Url.SubRouteUrl("www", "Home.Index")';
var editUserURL = '@Url.SubRouteUrl("profile", "Profile.Action", new { action = "Edit" })';
var deleteUserURL = '@Url.SubRouteUrl("profile", "Profile.Action", new { action = "Delete" })';
</script>
@Scripts.Render("~/bundles/profile")
@{
bool OwnProfile = (Model.Username == User.Identity.Name);
}
<div class="container">
@if (Model != null)
@if (!Model.Error)
{
bool OwnProfile = (Model.Username == User.Identity.Name);
<div class="row">
<div class="col-sm-3@((string.IsNullOrEmpty(Model.About) && !OwnProfile) ? " col-sm-offset-4" : string.Empty)"><h1>@Model.Username></h1></div>
</div>

1
Teknik/Areas/Profile/Views/Profile/Login.cshtml

@ -1,6 +1,7 @@ @@ -1,6 +1,7 @@
@model Teknik.Areas.Profile.ViewModels.LoginViewModel
<form role="form" id="loginForm" action="@Url.SubRouteUrl("profile", "Profile.Login")" method="post" accept-charset="UTF-8">
@Html.AntiForgeryToken()
<input name="ReturnUrl" id="ReturnUrl" type="hidden" value="@Model.ReturnUrl" />
<div class="form-group">
<input type="text" class="form-control" id="Username" value="" placeholder="Username" name="Username" data-val-required="The Username field is required." data-val="true" />

1
Teknik/Areas/Profile/Views/Profile/Register.cshtml

@ -1,6 +1,7 @@ @@ -1,6 +1,7 @@
@model Teknik.Areas.Profile.ViewModels.RegisterViewModel
<form role="form" id="registrationForm" action="@Url.SubRouteUrl("profile", "Profile.Register")" method="post" accept-charset="UTF-8">
@Html.AntiForgeryToken()
<input name="ReturnUrl" id="ReturnUrl" type="hidden" value="@Model.ReturnUrl" />
<div class="form-group">
<input type="text" class="form-control" id="Username" value="" placeholder="Username" name="Username" data-val-required="The Username field is required." data-val="true"/>

11
Teknik/Global.asax.cs

@ -44,13 +44,16 @@ namespace Teknik @@ -44,13 +44,16 @@ namespace Teknik
{
User user = entities.Users.Include("Groups").Include("Groups.Roles").SingleOrDefault(u => u.Username == username);
foreach (Group grp in user.Groups)
if (user != null)
{
foreach (Role role in grp.Roles)
foreach (Group grp in user.Groups)
{
if (!roles.Contains(role.Name))
foreach (Role role in grp.Roles)
{
roles.Add(role.Name);
if (!roles.Contains(role.Name))
{
roles.Add(role.Name);
}
}
}
}

9
Teknik/ViewModels/ViewModelBase.cs

@ -21,5 +21,14 @@ namespace Teknik.ViewModels @@ -21,5 +21,14 @@ namespace Teknik.ViewModels
return _config;
}
}
public bool Error { get; set; }
public string ErrorMessage { get; set; }
public ViewModelBase()
{
Error = false;
}
}
}
Loading…
Cancel
Save