C
C#15mo ago
wwww

✅ Some terrible code

Soooooooooooooooo, I have no clue how to make HTTP requests. Can anyone advise a nice guide? Like, that's what i have built with ChatGPT's help. I just need some advice, except to stop coding 🙂
38 Replies
Angius
Angius15mo ago
This looks good enough, what's the problem? Unless you just want to make it better in general?
wwww
wwwwOP15mo ago
Yep, something like this. Maybe anyone has some good guides about this topic cuz core 7 is kinda new as i get, and finding something useful is a bit harder then other things
Angius
Angius15mo ago
Well, one obvious improvement would be using dependency injection to get the database context instead of newing it up Far as guides go, Microsoft's docs are good enough for beginners
wwww
wwwwOP15mo ago
like this one?
Angius
Angius15mo ago
For example, yes
Angius
Angius15mo ago
Tutorial: Create a minimal API with ASP.NET Core
Learn how to build a minimal API with ASP.NET Core.
wwww
wwwwOP15mo ago
So, what do you mean? Are you suggesting that I could create custom middleware which, upon creation, initializes an instance of the database using a class constructor?
Angius
Angius15mo ago
No I mean what I said Use dependency injection Register the context in the services And inject it via a parameter in your lambda
wwww
wwwwOP15mo ago
like this?
Angius
Angius15mo ago
Almost EF has its own method of registering in the services
builder.Services.AddDbContext<MyCoolDbContext>();
builder.Services.AddDbContext<MyCoolDbContext>();
Then you inject it like you have it here, yes But with MyCoolDbContext not any interface
wwww
wwwwOP15mo ago
Oh, now it even works. Thank you very much!
Developful
Developful15mo ago
also why Persosn
Angius
Angius15mo ago
Also, why do you read the data from the httpcontext? You can just bind it
Developful
Developful15mo ago
is that a typo?
Angius
Angius15mo ago
No need to use request and response directly, it's not Node.js lol
wwww
wwwwOP15mo ago
fixed lmao
wwww
wwwwOP15mo ago
its only way i found how to do that
Angius
Angius15mo ago
async ([FromBody] Person person, UserBaseContext db) =>
{
// ...
if (...)
{
// ...
return TypedResults.Ok(user);
}
else
{
return TypedResults.NotFound("ERROR");
}
}
async ([FromBody] Person person, UserBaseContext db) =>
{
// ...
if (...)
{
// ...
return TypedResults.Ok(user);
}
else
{
return TypedResults.NotFound("ERROR");
}
}
There
wwww
wwwwOP15mo ago
wwww
wwwwOP15mo ago
app.MapPost("/login", async ([FromBody] Person person, UserBaseContext db) =>
{
if (person != null)
{
User? user = db.Users.FirstOrDefault(u => u.Username == person.Username);
if (user != null)
{
if (user.Password == person.Password)
{
user.Password = new string('*', user.Password.Length);
return TypedResults.Ok(user);
}
}
}
else
{
return TypedResults.NotFound("ERROR");
}
});
app.MapPost("/login", async ([FromBody] Person person, UserBaseContext db) =>
{
if (person != null)
{
User? user = db.Users.FirstOrDefault(u => u.Username == person.Username);
if (user != null)
{
if (user.Password == person.Password)
{
user.Password = new string('*', user.Password.Length);
return TypedResults.Ok(user);
}
}
}
else
{
return TypedResults.NotFound("ERROR");
}
});
im so dumb lmao rly can't get whats a problem here
Angius
Angius15mo ago
Well, you don't run any async code here So that await is kinda useless You should change FirstOrDefault() into FirstOrDefaultAsync() and await that
wwww
wwwwOP15mo ago
yep, thx :), but this thing is still here
Angius
Angius15mo ago
Where does this error happen?
wwww
wwwwOP15mo ago
Angius
Angius15mo ago
That's... very weird I guess you could try explicity addng a return type to this lambda...?
async Task<Results<Ok<User>, NotFound<string>>> ([FromBody ...
async Task<Results<Ok<User>, NotFound<string>>> ([FromBody ...
wwww
wwwwOP15mo ago
more errors lmao
wwww
wwwwOP15mo ago
nmv about that Thank you very much again I'll come up with something
Angius
Angius15mo ago
Task<IResult> maybe Not as type-safe but still Regardless, even the initial error, about RequestDelegate not taking 2 arguments, should not be a thing
wwww
wwwwOP15mo ago
wwww
wwwwOP15mo ago
now it works
Angius
Angius15mo ago
Nice
wwww
wwwwOP15mo ago
had do add last return but i don't think its important at all rn 🙂
wwww
wwwwOP15mo ago
wwww
wwwwOP15mo ago
this way betterr
Angius
Angius15mo ago
Oh yeah
wwww
wwwwOP15mo ago
Thanks again! You've saved me over 30 hours of googling time 🙂 badhun1UA
Angius
Angius15mo ago
Anytime Ok
Want results from more Discord servers?
Add your server