Skip to content

Commit 35a7131

Browse files
author
Kalyan Krishna
committed
Client side code framing complete
1 parent 3753857 commit 35a7131

File tree

14 files changed

+281
-69
lines changed

14 files changed

+281
-69
lines changed

4-WebApp-your-API/4-1-Your-API/Client/Controllers/TodoListController.cs

Lines changed: 81 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,73 @@
11
using System;
2+
using System.Collections;
23
using System.Collections.Generic;
34
using System.Linq;
45
using System.Threading.Tasks;
56
using Microsoft.AspNetCore.Http;
67
using Microsoft.AspNetCore.Mvc;
8+
using Microsoft.Graph;
79
using Microsoft.Identity.Web.Client;
10+
using TodoListService.Models;
11+
using TodoListClient.Utils;
812

913
namespace TodoListClient.Controllers
1014
{
1115
public class TodoListController : Controller
1216
{
1317
ITokenAcquisition _tokenAcquisition;
18+
IList<Todo> Model = new List<Todo>();
1419

1520
public TodoListController(ITokenAcquisition tokenAcquisition)
1621
{
1722
_tokenAcquisition = tokenAcquisition;
23+
24+
1825
}
1926

2027
// GET: TodoList
2128
public ActionResult Index()
2229
{
23-
return View();
30+
if (HttpContext.Session.Get<IList<Todo>>("ToDoList") == null)
31+
{
32+
Model.Add(new Todo() { Id = 1, Owner = "[email protected]", Title = "do something" });
33+
Model.Add(new Todo() { Id = 2, Owner = "[email protected]", Title = "do something else" });
34+
35+
HttpContext.Session.Set<IList<Todo>>("ToDoList", Model);
36+
}
37+
38+
Model = HttpContext.Session.Get<IList<Todo>>("ToDoList");
39+
40+
return View(Model);
2441
}
2542

2643
// GET: TodoList/Details/5
2744
public ActionResult Details(int id)
2845
{
29-
return View();
46+
Model = HttpContext.Session.Get<IList<Todo>>("ToDoList");
47+
48+
return View(Model.FirstOrDefault(x => x.Id == id));
3049
}
3150

3251
// GET: TodoList/Create
3352
public ActionResult Create()
3453
{
35-
return View();
54+
Todo todo = new Todo() { Owner = HttpContext.User.Identity.Name };
55+
return View(todo);
3656
}
3757

3858
// POST: TodoList/Create
3959
[HttpPost]
4060
[ValidateAntiForgeryToken]
41-
public ActionResult Create(IFormCollection collection)
61+
public ActionResult Create([Bind("Title,Owner")] Todo todo)
4262
{
4363
try
4464
{
45-
// TODO: Add insert logic here
65+
Model = HttpContext.Session.Get<IList<Todo>>("ToDoList");
66+
67+
int id = Model.OrderByDescending(x => x.Id).FirstOrDefault().Id + 1;
68+
69+
Model.Add(new Todo() { Id = id, Owner = HttpContext.User.Identity.Name, Title = todo.Title });
70+
HttpContext.Session.Set<IList<Todo>>("ToDoList", Model);
4671

4772
return RedirectToAction(nameof(Index));
4873
}
@@ -55,18 +80,41 @@ public ActionResult Create(IFormCollection collection)
5580
// GET: TodoList/Edit/5
5681
public ActionResult Edit(int id)
5782
{
58-
return View();
83+
Model = HttpContext.Session.Get<IList<Todo>>("ToDoList");
84+
85+
Todo todo = Model.FirstOrDefault(x => x.Id == id);
86+
87+
if (todo == null)
88+
{
89+
return NotFound();
90+
}
91+
92+
return View(todo);
5993
}
6094

6195
// POST: TodoList/Edit/5
6296
[HttpPost]
6397
[ValidateAntiForgeryToken]
64-
public ActionResult Edit(int id, IFormCollection collection)
98+
public ActionResult Edit(int id, [Bind("Id,Title,Owner")] Todo todo)
6599
{
66100
try
67101
{
68-
// TODO: Add update logic here
102+
if (id != todo.Id)
103+
{
104+
return NotFound();
105+
}
106+
107+
Model = HttpContext.Session.Get<IList<Todo>>("ToDoList");
108+
109+
if (Model.FirstOrDefault(x => x.Id == id) == null)
110+
{
111+
return NotFound();
112+
}
69113

114+
Model.Remove(Model.FirstOrDefault(x => x.Id == id));
115+
Model.Add(todo);
116+
117+
HttpContext.Session.Set<IList<Todo>>("ToDoList", Model);
70118
return RedirectToAction(nameof(Index));
71119
}
72120
catch
@@ -78,17 +126,39 @@ public ActionResult Edit(int id, IFormCollection collection)
78126
// GET: TodoList/Delete/5
79127
public ActionResult Delete(int id)
80128
{
81-
return View();
129+
Model = HttpContext.Session.Get<IList<Todo>>("ToDoList");
130+
131+
Todo todo = Model.FirstOrDefault(x => x.Id == id);
132+
133+
if (todo == null)
134+
{
135+
return NotFound();
136+
}
137+
138+
return View(todo);
82139
}
83140

84141
// POST: TodoList/Delete/5
85142
[HttpPost]
86143
[ValidateAntiForgeryToken]
87-
public ActionResult Delete(int id, IFormCollection collection)
144+
public ActionResult Delete(int id, [Bind("Id,Title,Owner")] Todo todo)
88145
{
89146
try
90147
{
91-
// TODO: Add delete logic here
148+
if (id != todo.Id)
149+
{
150+
return NotFound();
151+
}
152+
153+
Model = HttpContext.Session.Get<IList<Todo>>("ToDoList");
154+
155+
if (Model.FirstOrDefault(x => x.Id == id) == null)
156+
{
157+
return NotFound();
158+
}
159+
160+
Model.Remove(Model.FirstOrDefault(x => x.Id == id));
161+
HttpContext.Session.Set<IList<Todo>>("ToDoList", Model);
92162

93163
return RedirectToAction(nameof(Index));
94164
}

4-WebApp-your-API/4-1-Your-API/Client/Models/TodoItem.cs

Lines changed: 0 additions & 12 deletions
This file was deleted.

4-WebApp-your-API/4-1-Your-API/Client/Startup.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Microsoft.AspNetCore.Authorization;
1+
using Microsoft.AspNetCore.Authentication.AzureAD.UI;
2+
using Microsoft.AspNetCore.Authorization;
23
using Microsoft.AspNetCore.Builder;
34
using Microsoft.AspNetCore.Hosting;
45
using Microsoft.AspNetCore.Http;
@@ -25,6 +26,8 @@ public Startup(IConfiguration configuration)
2526
// This method gets called by the runtime. Use this method to add services to the container.
2627
public void ConfigureServices(IServiceCollection services)
2728
{
29+
services.AddDistributedMemoryCache();
30+
2831
services.Configure<CookiePolicyOptions>(options =>
2932
{
3033
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
@@ -33,11 +36,12 @@ public void ConfigureServices(IServiceCollection services)
3336
});
3437

3538
services.AddOptions();
39+
services.AddSession();
3640

3741
// Token acquisition service based on MSAL.NET
3842
// and chosen token cache implementation
3943
services.AddAzureAdV2Authentication(Configuration)
40-
.AddMsal(new string[] { Constants.ScopeUserImpersonation })
44+
.AddMsal(new string[] { Configuration["TodoList:TodoListScope"] })
4145
.AddInMemoryTokenCaches();
4246

4347
// Add APIs
@@ -68,8 +72,10 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env)
6872

6973
app.UseHttpsRedirection();
7074
app.UseStaticFiles();
75+
7176
app.UseCookiePolicy();
7277

78+
app.UseSession();
7379
app.UseAuthentication();
7480

7581
app.UseMvc(routes =>

4-WebApp-your-API/4-1-Your-API/Client/TodoListClient.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717
<None Remove="ReadmeFiles\**" />
1818
</ItemGroup>
1919

20+
<ItemGroup>
21+
<Compile Include="..\TodoListService\Models\TodoItem.cs" Link="Models\TodoItem.cs" />
22+
</ItemGroup>
23+
2024
<ItemGroup>
2125
<PackageReference Include="Microsoft.AspNetCore.App" />
2226
<PackageReference Include="Microsoft.AspNetCore.Authentication.AzureAD.UI" Version="2.2.0" />
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using Microsoft.AspNetCore.Http;
2+
using Newtonsoft.Json;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Threading.Tasks;
7+
8+
namespace TodoListClient.Utils
9+
{
10+
public static class SessionExtensions
11+
{
12+
public static void Set<T>(this ISession session, string key, T value)
13+
{
14+
session.SetString(key, JsonConvert.SerializeObject(value));
15+
}
16+
17+
public static T Get<T>(this ISession session, string key)
18+
{
19+
var value = session.GetString(key);
20+
21+
return value == null ? default(T) :
22+
JsonConvert.DeserializeObject<T>(value);
23+
}
24+
}
25+
}

4-WebApp-your-API/4-1-Your-API/Client/Views/Shared/_Layout.cshtml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,7 @@
3131
<div class="navbar-collapse collapse">
3232
<ul class="nav navbar-nav">
3333
<li><a asp-area="" asp-controller="Home" asp-action="Index">Home</a></li>
34-
<li><a asp-area="" asp-controller="Home" asp-action="Profile">Profile</a></li>
35-
<li><a asp-area="" asp-controller="Home" asp-action="Tenants">Tenants</a></li>
36-
<li><a asp-area="" asp-controller="Home" asp-action="Blob">Blob</a></li>
34+
<li><a asp-area="" asp-controller="TodoList" asp-action="Index">TodoList</a></li>
3735
</ul>
3836
<partial name="_LoginPartial" />
3937
</div>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
@model TodoListService.Models.Todo
2+
@{
3+
ViewData["Title"] = "Create";
4+
}
5+
6+
<h2>Create ToDo</h2>
7+
8+
<h4>Todo</h4>
9+
<hr />
10+
<div class="row">
11+
<div class="col-md-4">
12+
<form asp-action="Create">
13+
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
14+
<div class="form-group">
15+
<label asp-for="Title" class="control-label"></label>
16+
<input asp-for="Title" class="form-control" />
17+
<span asp-validation-for="Title" class="text-danger"></span>
18+
</div>
19+
<div class="form-group">
20+
<label asp-for="Owner" class="control-label"></label>
21+
<input asp-for="Owner" class="form-control" value="@Html.DisplayTextFor(model => model.Owner)"/>
22+
<span asp-validation-for="Owner" class="text-danger"></span>
23+
</div>
24+
<div class="form-group">
25+
<input type="submit" value="Create" class="btn btn-primary" />
26+
</div>
27+
</form>
28+
</div>
29+
</div>
30+
31+
<div>
32+
<a asp-action="Index">Back to List</a>
33+
</div>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
@model TodoListService.Models.Todo
2+
3+
@{
4+
ViewData["Title"] = "Delete";
5+
}
6+
7+
<h1>Delete ToDo</h1>
8+
9+
<h3>Are you sure you want to delete this?</h3>
10+
<div>
11+
<h4>Todo</h4>
12+
<hr />
13+
<dl class="row">
14+
<dt class="col-sm-2">
15+
@Html.DisplayNameFor(model => model.Title)
16+
</dt>
17+
<dd class="col-sm-10">
18+
@Html.DisplayFor(model => model.Title)
19+
</dd>
20+
<dt class="col-sm-2">
21+
@Html.DisplayNameFor(model => model.Owner)
22+
</dt>
23+
<dd class="col-sm-10">
24+
@Html.DisplayFor(model => model.Owner)
25+
</dd>
26+
</dl>
27+
28+
<form asp-action="Delete">
29+
<input type="hidden" asp-for="Id" />
30+
<input type="submit" value="Delete" class="btn btn-danger" /> |
31+
<a asp-action="Index">Back to List</a>
32+
</form>
33+
</div>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
@model TodoListService.Models.Todo
2+
3+
@{
4+
ViewData["Title"] = "Details";
5+
}
6+
7+
<h2>ToDo Details</h2>
8+
9+
<p>
10+
<a asp-action="Create">Create New</a>
11+
</p>
12+
<table class="table">
13+
<thead>
14+
<tr>
15+
<th>
16+
@Html.DisplayFor(model => model.Id)
17+
</th>
18+
<th></th>
19+
</tr>
20+
</thead>
21+
<tbody>
22+
<tr>
23+
<td>
24+
@Html.DisplayFor(model => model.Owner)
25+
</td>
26+
<td>
27+
@Html.DisplayFor(model => model.Title)
28+
</td>
29+
</tr>
30+
</tbody>
31+
</table>

0 commit comments

Comments
 (0)