C
C#10mo 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
Angius10mo ago
This looks good enough, what's the problem? Unless you just want to make it better in general?
wwww
wwww10mo 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
Angius10mo 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
wwww10mo ago
like this one?
Angius
Angius10mo ago
For example, yes
Angius
Angius10mo ago
Tutorial: Create a minimal API with ASP.NET Core
Learn how to build a minimal API with ASP.NET Core.
wwww
wwww10mo 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
Angius10mo 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
wwww10mo ago
like this?
Angius
Angius10mo 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
wwww10mo ago
Oh, now it even works. Thank you very much!
Developful
Developful10mo ago
also why Persosn
Angius
Angius10mo ago
Also, why do you read the data from the httpcontext? You can just bind it
Developful
Developful10mo ago
is that a typo?
Angius
Angius10mo ago
No need to use request and response directly, it's not Node.js lol
wwww
wwww10mo ago
fixed lmao
wwww
wwww10mo ago
its only way i found how to do that
Angius
Angius10mo 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
wwww10mo ago
wwww
wwww10mo 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
Angius10mo 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
wwww10mo ago
yep, thx :), but this thing is still here
Angius
Angius10mo ago
Where does this error happen?
wwww
wwww10mo ago
Angius
Angius10mo 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
wwww10mo ago
more errors lmao
wwww
wwww10mo ago
nmv about that Thank you very much again I'll come up with something
Angius
Angius10mo 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
wwww10mo ago
wwww
wwww10mo ago
now it works
Angius
Angius10mo ago
Nice
wwww
wwww10mo ago
had do add last return but i don't think its important at all rn 🙂
wwww
wwww10mo ago
wwww
wwww10mo ago
this way betterr
Angius
Angius10mo ago
Oh yeah
wwww
wwww10mo ago
Thanks again! You've saved me over 30 hours of googling time 🙂 badhun1UA
Angius
Angius10mo ago
Anytime Ok
Want results from more Discord servers?
Add your server
More Posts
❔ StreamReader and File.WriteAllText are stepping on each others toes.I'm currently writing what I thought was a pretty straightforward piece of code. First I read a bunc❔ simple fft library for analyzing music?im new at C# coming from java where i used to use a library called QuiFFT which has one liners for r❔ ✅ How to debug "More than twenty 'IServiceProvider' instances have been created ... "?Hi, so I started getting this error after I connected my backend to my frontend. It didn't appear be❔ Cors Policy ErrorWhen ever my front-end tries to communicate with my back-end I receive this error, any ideas why? h❔ How to bring in DbContext?```csharp app.MapPost("/cities", async ([FromBody] City city) => { ... }); ```❔ ✅ How do you structure AzureFunctionApp project?I have azure function app project with few functions, logic and models are contained in folder with ❔ Proper email backround service archHi, need help. I have a booking app in which I will send confirmation emails, thank you emails, remi❔ Getting documentation from another class and putting it in another oneTitle is probably very confusing, but I'm asking if theres a way to do this ```cs /// <summary> ///❔ Creating a server for a messaging appFor a messaging app I'm developing, I've created a contacts collection (code below) which will conta✅ Correct MVVM approach for displaying models representing settings**Given** that I have a set of models representing settings, **When** I supply then to a UI utilizi