Skip to content
This repository has been archived by the owner on Jun 22, 2021. It is now read-only.

Commit

Permalink
Merge pull request #2 from MetzinAround/Update-Add-Poet-Page-Jankiness
Browse files Browse the repository at this point in the history
Update add poet page jankiness
  • Loading branch information
MetzinAround authored Dec 27, 2020
2 parents f0805f3 + 6aa44ff commit 11c401f
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 34 deletions.
24 changes: 18 additions & 6 deletions Your New Favorite Poem/Pages/AddPoet.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,31 @@
ViewData["Title"] = "Add a Poet";
}
<body>
<form method="post" asp-page="AddPoet">
<form method="post" asp-page="AddPoet">
<section class="Author">
<label for="Author">What is the name of the poet?</label>
<input type="text" name="author" id="author">
<label for="AuthorName">What is the name of the poet?</label>
<input type="text" name="authorName" id="authorName">
</section>
<section class="Poem">
<label for="Poem">What is the url for the poem?</label>
<input type="text" name="poem" id="poem">
<section class="PoemUrl">
<label for="PoemUrl">What is the url for the poem?</label>
<input type="text" name="poemUrl" id="poemUrl">
</section>
<section class="PoemName">
<label for="PoemName">What is the name of the poem?</label>
<input type="text" name="poemName" id="poemName">
</section>
<section class="PictureUrl">
<label for="PictureUrl">Please add a picture URL of the poet.</label>
<input type="text" name="pictureUrl" id="pictureUrl">
</section>
<section class="PictureAltText">
<label for="PictureAltText">Please describe the picture to use as Alt text.</label>
<input type="text" name="pictureAltText" id="pictureAltText">
</section>
<section class="Bio">
<label for="Bio">Please write a short biography of the poet.</label>
<textarea type="text" name="bio" id="bio"></textarea>
</section>
<section class="submission">
<input type="submit" value="Submit Poem" asp-page-handler="Submit">
</section>
Expand Down
62 changes: 44 additions & 18 deletions Your New Favorite Poem/Pages/AddPoet.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,64 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Logging;
using Renci.SshNet;
using Your_New_Favorite_Poem.Database;
using Your_New_Favorite_Poem.Models;

namespace Your_New_Favorite_Poem.Pages
//This is not compiling in order to allow us to get the database working. We made an error on this page on purpose.
{
public class AddPoetModel : PageModel
{
private readonly AuthorsDbContext _authorsDbContext;
private readonly ILogger<AddPoetModel> _logger;

public AddPoetModel(ILogger<AddPoetModel> logger, AuthorsDbContext authorsDbContext)
{
_logger = logger;
_authorsDbContext = authorsDbContext;

}


public string SubmissionResult { get; private set; } = "Submit your poem above!";
public async Task OnPostSubmit(string author, string poem, string poemName)
public async Task OnPostSubmit(string authorName, string poemUrl, string poemName, string bio, string pictureUrl, string pictureAltText )
{
var isValidUri = Uri.TryCreate(poem, UriKind.Absolute, out var poemUri);
if (!isValidUri || poemUri is null)
var isPoemUrlValid = Uri.TryCreate(poemUrl, UriKind.Absolute, out var poemUri);
var isPictureUrlValid = Uri.TryCreate(pictureUrl, UriKind.Absolute, out var pictureUri);
if (!isPoemUrlValid || poemUri is null)
{
SubmissionResult = "Invalid URL";
SubmissionResult = "Invalid Poem URL";
}
else
else if (!isPictureUrlValid || pictureUri is null)
{
SubmissionResult = "Invalid Picture URL";
} else
{
try
{
// await _poemDatabase.InsertData(new Poem(author, poemName, poemUri));
#if RELEASE
#error "You big idiot."
#endif
throw new Exception("CODEPATH TEMPORARILY BROKEN; Please fix me");
var givenAuthor = new Author
{
IsVerified = false,
PictureAltText = pictureAltText,
Bio = bio,
Name = authorName,
PictureURL = pictureUri,
Poems = new List<Poem>
{
new Poem()
{
URL = poemUri,
Title = poemName,
IsVerified = false
}
}

};

await _authorsDbContext.AddAsync<Author>(givenAuthor);
await _authorsDbContext.SaveChangesAsync();

SubmissionResult = "We did it! Submission Accepted. Check back soon!";
}
catch
Expand All @@ -38,14 +72,6 @@ public async Task OnPostSubmit(string author, string poem, string poemName)
}
}

public AddPoetModel(ILogger<AddPoetModel> logger, AuthorsDbContext authorsDbContext)
{
_logger = logger;
_authorsDbContext = authorsDbContext;

}
private readonly AuthorsDbContext _authorsDbContext;
private readonly ILogger<AddPoetModel> _logger;
public void OnGet()
{

Expand Down
4 changes: 2 additions & 2 deletions Your New Favorite Poem/Pages/Index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@
</div>
<!--Index page as a single foreach loop-->

@foreach (var author in @Model.AuthorsFromDatabase)
@foreach (var author in @Model.AuthorsFromDatabase.Where(x => x.IsVerified is true))
{
//poems and links not displaying after reseeding database
<section class="poets">
<h2>@author.Name </h2>
<br />
<ul>
@foreach (var poem in author.Poems)
@foreach (var poem in author.Poems.Where(x => x.IsVerified is true))
{

<li><a href=@poem.URL target="_blank">@poem.Title</a></li>
Expand Down
4 changes: 2 additions & 2 deletions Your New Favorite Poem/Pages/Poets.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
<nav>
<!-- fullist of poets-->
<ul>
@foreach (var author in Model.PoemsFromDatabase.Where(x => x.IsVerified).Select(x => x.Author.Name).Distinct())
@foreach (var authorName in Model.AuthorsFromDatabase.Where(x => x.IsVerified).Select(x => x.Name).Distinct())
{

<li><a asp-area="" asp-page="/Index" asp-fragment=@author>@author</a></li>
<li><a asp-area="" asp-page="/Index" asp-fragment=@authorName>@authorName</a></li>

}
</ul>
Expand Down
9 changes: 3 additions & 6 deletions Your New Favorite Poem/Pages/Poets.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,19 @@ namespace Your_New_Favorite_Poem.Pages

public class PoetsModel : PageModel
{
public IReadOnlyList<Poem> PoemsFromDatabase { get; }
public IReadOnlyList<Author> AuthorsFromDatabase { get; private set; } = Array.Empty<Author>();
readonly AuthorsDbContext _authorsDbContext;
public PoetsModel(ILogger<PoetsModel> logger, AuthorsDbContext authorsDbContext)
{
Logger = logger;
_authorsDbContext = authorsDbContext;
var temp = authorsDbContext.Authors.ToList();
PoemsFromDatabase = authorsDbContext.Poems.ToList();
}

internal ILogger<PoetsModel> Logger { get; }

public void OnGet()
{ //requires asnyc to work
//Poem savedPoem = await _poemDatabase.InsertPoems(new Poem("name", "title", new Uri("url")));
//Console.WriteLine($"Save Successful! Poem Id {savedPoem.Id}");
{
AuthorsFromDatabase = _authorsDbContext.Authors.ToList();
}
}
}
1 change: 1 addition & 0 deletions Your New Favorite Poem/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using Your_New_Favorite_Poem.Database;

namespace Your_New_Favorite_Poem

{
public class Startup
{
Expand Down

0 comments on commit 11c401f

Please sign in to comment.