Skip to content

Commit dd69cc4

Browse files
authored
Let's add some improvements to chirp! (#120)
* added option to reset the contents of the tables * automatically adds user as author to prevent error * very long-worded cheeps break the word to wrap * removed a lot of duplicate code in timeline models * changed alert message * Further reduced redundancy and redundant code * added some EOFs
1 parent e7d4f38 commit dd69cc4

File tree

12 files changed

+129
-190
lines changed

12 files changed

+129
-190
lines changed

src/Chirp.Web/Pages/Auth.cshtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
@page "/login/oauth/auth"
2-
@model Chirp.Web.Pages.Auth
2+
@model Chirp.Web.Pages.AuthModel
33

44
@{
55
Layout = null;

src/Chirp.Web/Pages/Auth.cshtml.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
using Microsoft.AspNetCore.Authorization;
2-
using Microsoft.AspNetCore.Mvc;
3-
using Microsoft.AspNetCore.Mvc.RazorPages;
42

53
namespace Chirp.Web.Pages;
64

75
[Authorize]
8-
public class Auth : PageModel
6+
public class AuthModel : PageModel
97
{
108
public ActionResult OnGet(bool? signOut)
119
{

src/Chirp.Web/Pages/GlobalUsings.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
global using Microsoft.AspNetCore.Mvc;
2+
global using Microsoft.AspNetCore.Mvc.RazorPages;
3+
global using Chirp.Infrastructure;
4+
global using Chirp.Core;

src/Chirp.Web/Pages/Public.cshtml

Lines changed: 2 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,11 @@
11
@page "/"
2-
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
3-
@model Chirp.Web.Pages.PublicModel
2+
@model Chirp.Web.Pages.TimelineModel
43
@{
54
ViewData["Title"] = "Chirp!";
65
Layout = "Shared/_Layout";
76
}
87

98
<div>
109
<h2> Public Timeline </h2>
11-
12-
@if (User.Identity.IsAuthenticated)
13-
{
14-
<div class="cheepbox">
15-
<h3>What's on your mind, @(User.Identity.Name)?</h3>
16-
<form method="post">
17-
<input style="float: left" asp-for="Text">
18-
<input type="submit">
19-
</form>
20-
</div>
21-
}
22-
@if (Model.Cheeps.Any())
23-
{
24-
<ul id="messagelist" class="cheeps">
25-
@foreach (var cheep in Model.Cheeps)
26-
{
27-
<li>
28-
<p>
29-
<strong>
30-
<a href="/@cheep.Author.Name">@cheep.Author.Name</a>
31-
</strong>
32-
@cheep.Text
33-
<small>&mdash; @cheep.GetSerializedTimeStamp()</small>
34-
</p>
35-
</li>
36-
}
37-
</ul>
38-
}
39-
else
40-
{
41-
<em>There are no cheeps so far.</em>
42-
}
43-
44-
<div class="pagination">
45-
@for (int i = 1; i <= Math.Ceiling((double) Model.NumOfCheeps / Model.CheepsPerPage); i++)
46-
{
47-
<a href="/?page=@i">@i</a>
48-
}
49-
</div>
10+
@await Html.PartialAsync("Shared/_CheepList")
5011
</div>

src/Chirp.Web/Pages/Public.cshtml.cs

Lines changed: 0 additions & 63 deletions
This file was deleted.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
@page "/reset"
2+
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
3+
@model Chirp.Web.Pages.ResetTablesModel;
4+
5+
@{
6+
Layout = "Shared/_Layout";
7+
}
8+
9+
<h2>Reset Tables</h2>
10+
11+
<form method="post" onsubmit="return confirm('Are you sure you want to reset the tables?');">
12+
<button type="submit" >Reset Tables</button>
13+
</form>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
namespace Chirp.Web.Pages;
2+
3+
public class ResetTablesModel : PageModel
4+
{
5+
6+
private ChirpDBContext _db;
7+
public ResetTablesModel(ChirpDBContext db)
8+
{
9+
_db = db;
10+
}
11+
public IActionResult OnPost()
12+
{
13+
_db.Cheeps.RemoveRange(_db.Cheeps);
14+
_db.Authors.RemoveRange(_db.Authors);
15+
_db.SaveChanges();
16+
17+
DbInitializer.SeedDatabase(_db);
18+
19+
return RedirectToPage("Public");
20+
}
21+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
2+
3+
@if (User.Identity.IsAuthenticated)
4+
{
5+
<div class="cheepbox">
6+
<h3>What's on your mind, @(User.Identity.Name)?</h3>
7+
<form method="post">
8+
<input style="float: left" name="Text">
9+
<input type="submit">
10+
</form>
11+
</div>
12+
}
13+
14+
@if (Model.Cheeps.Count > 0)
15+
{
16+
<ul id="messagelist" class="cheeps">
17+
@foreach (var cheep in Model.Cheeps)
18+
{
19+
<li>
20+
<p>
21+
<strong>
22+
<a href="/@cheep.Author.Name">@cheep.Author.Name</a>
23+
</strong>
24+
@cheep.Text
25+
<small>&mdash; @cheep.GetSerializedTimeStamp()</small>
26+
</p>
27+
</li>
28+
}
29+
</ul>
30+
}
31+
else
32+
{
33+
<em>There are no cheeps so far.</em>
34+
}
35+
36+
<div class="pagination">
37+
@for (int i = 1; i <= Math.Ceiling((double)Model.NumOfCheeps / Model.CheepsPerPage); i++)
38+
{
39+
if (Context.GetEndpoint().DisplayName.Contains("Public"))
40+
{
41+
<a href="/?page=@i">@i</a>
42+
}
43+
else
44+
{
45+
<a href="?page=@i">@i</a>
46+
}
47+
}
48+
</div>
Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,41 @@
11
<!DOCTYPE html>
22
<html lang="en">
3+
34
<head>
45
<title>@ViewData["Title"]</title>
56
<link href="/css/style.css" rel="stylesheet" type="text/css" />
67
<link rel="icon" type="image/png" href="/favicon/favicon.ico">
78
</head>
9+
810
<body>
9-
<div class=page>
10-
<h1><img src="/images/icon1.png" alt="Icon1"/>Chirp!</h1>
11-
<div class=navigation>
11+
<div class=page>
12+
<h1><img src="/images/icon1.png" alt="Icon1" />Chirp!</h1>
13+
<div class=navigation>
1214
@if (User.Identity.IsAuthenticated)
1315
{
1416
<div>
1517
<a href="/@(User.Identity.Name)">my timeline</a> |
1618
<a href="/">public timeline</a> |
17-
<a href="/login/oauth/auth?signOut=true" >logout [@(User.Identity.Name)]</a>
19+
<a href="/reset">reset tables</a> |
20+
<a href="/login/oauth/auth?signOut=true">logout [@(User.Identity.Name)]</a>
1821
</div>
1922
}
2023
else
2124
{
2225
<div>
2326
<a href="/">public timeline</a> |
24-
<a href="/login/oauth/auth" >login</a>
27+
<a href="/login/oauth/auth">login</a>
2528
</div>
2629
}
2730
</div>
28-
<div class=body>
29-
@RenderBody()
30-
</div>
31-
<div class=footer>
32-
Chirp &mdash; An ASP.NET Application
31+
<div class=body>
32+
@RenderBody()
33+
</div>
34+
<div class=footer>
35+
Chirp &mdash; An ASP.NET Application
36+
</div>
3337
</div>
34-
</div>
3538

3639
</body>
37-
</html>
40+
41+
</html>
Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,37 @@
1-
using Microsoft.AspNetCore.Authorization;
2-
using Microsoft.AspNetCore.Mvc;
3-
using Microsoft.AspNetCore.Mvc.RazorPages;
4-
using Chirp.Core;
5-
using Chirp.Infrastructure;
6-
71
namespace Chirp.Web.Pages;
82

9-
public class UserTimelineModel : PageModel
3+
public class TimelineModel : PageModel
104
{
11-
private readonly ICheepService _service;
125
public List<Cheep> Cheeps { get; set; } = new List<Cheep>();
136
public int CheepsPerPage;
147
public int NumOfCheeps;
15-
private ChirpDBContext _db;
16-
private readonly ICheepRepository _cheepRepository;
17-
private readonly IAuthorRepository _authorRepository;
18-
public UserTimelineModel(ChirpDBContext db, ICheepRepository cheepRepository, IAuthorRepository authorRepository, ICheepService service)
8+
protected readonly ICheepService _service;
9+
protected ChirpDBContext _db;
10+
protected readonly ICheepRepository _cheepRepository;
11+
protected readonly IAuthorRepository _authorRepository;
12+
public TimelineModel(ChirpDBContext db, ICheepRepository cheepRepository, IAuthorRepository authorRepository, ICheepService service)
1913
{
2014
_db = db;
2115
_cheepRepository = cheepRepository;
2216
_authorRepository = authorRepository;
2317
_service = service;
2418
}
2519

26-
public ActionResult OnGet(string author, [FromQuery] int page = 1)
20+
public async Task<IActionResult> OnPostAsync()
2721
{
22+
string text = Request.Form["Text"];
23+
if (text.Length > 180) text = text.Substring(0, 180);
2824

25+
_authorRepository.CreateAuthor(User.Identity.Name, "example@mail.com");
26+
var authorId = _authorRepository.FindAuthorsByName(User.Identity.Name).First().AuthorId;
27+
var newCheepId = _db.Cheeps.Max(cheep => cheep.CheepId) + 1;
28+
_cheepRepository.StoreCheep(new Cheep { AuthorId = authorId, CheepId = newCheepId, Text = text, TimeStamp = DateTime.Now });
29+
return RedirectToPage();
30+
31+
}
32+
33+
public ActionResult OnGet(string? author, [FromQuery] int page = 1)
34+
{
2935
NumOfCheeps = _service.GetCheepCount(author);
3036

3137
int maxPage = (int) Math.Ceiling((double) NumOfCheeps / _service.CheepsPerPage);
@@ -35,7 +41,7 @@ public ActionResult OnGet(string author, [FromQuery] int page = 1)
3541
page = 1;
3642
}
3743

38-
if (page < 1 || page > maxPage)
44+
if ((page < 1 || page > maxPage) && _cheepRepository.QueryCheepCount(author) != 0)
3945
{
4046
return RedirectToPage();
4147
}
@@ -44,20 +50,4 @@ public ActionResult OnGet(string author, [FromQuery] int page = 1)
4450
CheepsPerPage = _service.CheepsPerPage;
4551
return Page();
4652
}
47-
48-
49-
50-
[BindProperty]
51-
public string? Text { get; set; }
52-
53-
public async Task<IActionResult> OnPostAsync()
54-
{
55-
if (Text.Length > 180) Text = Text.Substring(0, 180);
56-
57-
_authorRepository.CreateAuthor(User.Identity.Name, "example@mail.com");
58-
var authorId = _authorRepository.FindAuthorsByName(User.Identity.Name).First().AuthorId;
59-
var newCheepId = _db.Cheeps.Max(cheep => cheep.CheepId) + 1;
60-
_cheepRepository.StoreCheep(new Cheep { AuthorId = authorId, CheepId = newCheepId, Text = Text, TimeStamp = DateTime.Now });
61-
return RedirectToPage();
62-
}
6353
}

0 commit comments

Comments
 (0)