Skip to content

Commit

Permalink
Commit models created and startup.cs modified
Browse files Browse the repository at this point in the history
  • Loading branch information
robson-paproski committed Oct 14, 2017
1 parent 06dced8 commit dd4b880
Show file tree
Hide file tree
Showing 8 changed files with 144 additions and 19 deletions.
28 changes: 26 additions & 2 deletions WebExample/Code/ModalSize.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,34 @@
using System;
namespace WebExample.Code
{
public class ModalSize
public enum ModalSize
{
public ModalSize()
Small,
Large,
Medium
}

public class BootstrapModel
{
public string ID { get; set; }
public string AreaLabeledId { get; set; }
public ModalSize Size { get; set; }
public string Message { get; set; }
public string ModalSizeClass
{
get
{
switch (this.Size)
{
case ModalSize.Small:
return "modal-sm";
case ModalSize.Large:
return "modal-lg";
case ModalSize.Medium:
default:
return "";
}
}
}
}
}
103 changes: 100 additions & 3 deletions WebExample/Controllers/ApplicationRoleController.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,107 @@
using System;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using WebExample.Models;
using IdentityRole = AspNetCore.Identity.PostgreSQL.IdentityRole;
namespace WebExample.Controllers
{
public class ApplicationRoleController
public class ApplicationRoleController : Controller
{
public ApplicationRoleController()
private readonly RoleManager<IdentityRole> roleManager;

public ApplicationRoleController(RoleManager<IdentityRole> roleManager)
{
this.roleManager = roleManager;
}

[HttpGet]
public IActionResult Index()
{
List<ApplicationRoleListViewModel> model = new List<ApplicationRoleListViewModel>();
model = roleManager.Roles.Select(r => new ApplicationRoleListViewModel
{
RoleName = r.Name,
Id = r.Id,
Description = r.Name,
//NumberOfUsers = r.Users.Count
}).ToList();
return View(model);
}
[HttpGet]
public async Task<IActionResult> AddEditApplicationRole(string id)
{
ApplicationRoleViewModel model = new ApplicationRoleViewModel();
if (!String.IsNullOrEmpty(id))
{
IdentityRole applicationRole = await roleManager.FindByIdAsync(id);
if (applicationRole != null)
{
model.Id = applicationRole.Id;
model.RoleName = applicationRole.Name;
// model.Description = applicationRole.Description;
}
}
return PartialView("_AddEditApplicationRole", model);
}
[HttpPost]
public async Task<IActionResult> AddEditApplicationRole(string id, ApplicationRoleViewModel model)
{
if (ModelState.IsValid)
{
bool isExist = !String.IsNullOrEmpty(id);
IdentityRole applicationRole = isExist ? await roleManager.FindByIdAsync(id) :
new IdentityRole
{
//CreatedDate = DateTime.UtcNow
};
applicationRole.Name = model.RoleName;
/* applicationRole.Description = model.Description;
applicationRole.IPAddress = Request.HttpContext.Connection.RemoteIpAddress.ToString();*/
IdentityResult roleRuslt = isExist ? await roleManager.UpdateAsync(applicationRole)
: await roleManager.CreateAsync(applicationRole);
if (roleRuslt.Succeeded)
{
return RedirectToAction("Index");
}
}
return RedirectToAction("Index");
}

[HttpGet]
public async Task<IActionResult> DeleteApplicationRole(string id)
{
string name = string.Empty;
if (!String.IsNullOrEmpty(id))
{
IdentityRole applicationRole = await roleManager.FindByIdAsync(id);
if (applicationRole != null)
{
name = applicationRole.Name;
}
}
return PartialView("_DeleteApplicationRole", name);
}

[HttpPost]
public async Task<IActionResult> DeleteApplicationRole(string id, FormCollection form)
{
if (!String.IsNullOrEmpty(id))
{
IdentityRole applicationRole = await roleManager.FindByIdAsync(id);
if (applicationRole != null)
{
IdentityResult roleRuslt = roleManager.DeleteAsync(applicationRole).Result;
if (roleRuslt.Succeeded)
{
return RedirectToAction("Index");
}
}
}
return View();
}
}
}
7 changes: 4 additions & 3 deletions WebExample/Models/ApplicationRoleListViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ namespace WebExample.Models
{
public class ApplicationRoleListViewModel
{
public ApplicationRoleListViewModel()
{
}
public Guid Id { get; set; }
public string RoleName { get; set; }
public string Description { get; set; }
public int NumberOfUsers { get; set; }
}
}
8 changes: 5 additions & 3 deletions WebExample/Models/ApplicationRoleViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using System;
using System.ComponentModel.DataAnnotations;
namespace WebExample.Models
{
public class ApplicationRoleViewModel
{
public ApplicationRoleViewModel()
{
}
public Guid Id { get; set; }
[Display(Name = "Role Name")]
public string RoleName { get; set; }
public string Description { get; set; }
}
}
8 changes: 5 additions & 3 deletions WebExample/Models/ModalFooter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ namespace WebExample.Models
{
public class ModalFooter
{
public ModalFooter()
{
}
public string SubmitButtonText { get; set; } = "Submit";
public string CancelButtonText { get; set; } = "Cancel";
public string SubmitButtonID { get; set; } = "btn-submit";
public string CancelButtonID { get; set; } = "btn-cancel";
public bool OnlyCancelButton { get; set; }
}
}
4 changes: 1 addition & 3 deletions WebExample/Models/ModalHeader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ namespace WebExample.Models
{
public class ModalHeader
{
public ModalHeader()
{
}
public string Heading { get; set; }
}
}
1 change: 1 addition & 0 deletions WebExample/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public void ConfigureServices(IServiceCollection services)
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddUserStore<UserStore<ApplicationUser>> ()
.AddRoleStore<RoleStore<IdentityRole>> ()
.AddRoleManager<RoleManager<IdentityRole>>()
.AddDefaultTokenProviders();

// Add application services.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
@model ApplicationRoleViewModel
@using IdentitySampleApplication.Models
@using WebExample.Models

<form asp-action="AddEditApplicationRole" role="form">
@await Html.PartialAsync("_ModalHeader", new ModalHeader { Heading =$"{(String.IsNullOrEmpty(Model.Id)? "Add" : "Edit")} Application Role" })
@await Html.PartialAsync("_ModalHeader", new ModalHeader { Heading =$"{(String.IsNullOrEmpty(Model.Id.ToString())? "Add" : "Edit")} Application Role" })
<div class="modal-body form-horizontal">
<div class="row">
<div class="form-group">
Expand Down

0 comments on commit dd4b880

Please sign in to comment.