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
-
7
1
namespace Chirp . Web . Pages ;
8
2
9
- public class UserTimelineModel : PageModel
3
+ public class TimelineModel : PageModel
10
4
{
11
- private readonly ICheepService _service ;
12
5
public List < Cheep > Cheeps { get ; set ; } = new List < Cheep > ( ) ;
13
6
public int CheepsPerPage ;
14
7
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 )
19
13
{
20
14
_db = db ;
21
15
_cheepRepository = cheepRepository ;
22
16
_authorRepository = authorRepository ;
23
17
_service = service ;
24
18
}
25
19
26
- public ActionResult OnGet ( string author , [ FromQuery ] int page = 1 )
20
+ public async Task < IActionResult > OnPostAsync ( )
27
21
{
22
+ string text = Request . Form [ "Text" ] ;
23
+ if ( text . Length > 180 ) text = text . Substring ( 0 , 180 ) ;
28
24
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
+ {
29
35
NumOfCheeps = _service . GetCheepCount ( author ) ;
30
36
31
37
int maxPage = ( int ) Math . Ceiling ( ( double ) NumOfCheeps / _service . CheepsPerPage ) ;
@@ -35,7 +41,7 @@ public ActionResult OnGet(string author, [FromQuery] int page = 1)
35
41
page = 1 ;
36
42
}
37
43
38
- if ( page < 1 || page > maxPage )
44
+ if ( ( page < 1 || page > maxPage ) && _cheepRepository . QueryCheepCount ( author ) != 0 )
39
45
{
40
46
return RedirectToPage ( ) ;
41
47
}
@@ -44,20 +50,4 @@ public ActionResult OnGet(string author, [FromQuery] int page = 1)
44
50
CheepsPerPage = _service . CheepsPerPage ;
45
51
return Page ( ) ;
46
52
}
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
- }
63
53
}
0 commit comments