packetphysicist
packetphysicist
CC#
Created by packetphysicist on 10/21/2024 in #help
✅ Unclear file structure
+---.github | ---workflows ---API +---Controllers +---Data +---Filters +---Middleware +---Migrations +---Models +---obj +---Options +---Properties +---RESTFiles +---Services This is my current folder structure. It feels very unclear and way too messy. How can I clean this up? I was thinking about putting Filters, Middleware and Options in the same folder as they're related. I wasn't sure how to name the parent folder of that though. I have about 9 controllers, the same amount in services and a few more models.
31 replies
CC#
Created by packetphysicist on 10/8/2024 in #help
✅ EF not creating all tables from migration
So I can use dotnet ef migrations add <name>. It says it successfully created a migration. Then I use: dotnet ef database update and itll create a database with a _EFMigrationsHistory table but not any of the other tables that I actually would like to use.
4 replies
CC#
Created by packetphysicist on 10/7/2024 in #help
Returning Service result to Controller (Minimal API)
/// <summary>
/// Deletes a reservation by its ID.
/// </summary>
/// <param name="reservationId">ID of the reservation to delete.</param>
public async Task Delete(int reservationId)
{
try
{
var reservation = await _context.Reservations
.FirstOrDefaultAsync(r => r.ReservationId == reservationId);

if (reservation is not null)
{
_context.Reservations.Remove(reservation);
await _context.SaveChangesAsync();
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
/// <summary>
/// Deletes a reservation by its ID.
/// </summary>
/// <param name="reservationId">ID of the reservation to delete.</param>
public async Task Delete(int reservationId)
{
try
{
var reservation = await _context.Reservations
.FirstOrDefaultAsync(r => r.ReservationId == reservationId);

if (reservation is not null)
{
_context.Reservations.Remove(reservation);
await _context.SaveChangesAsync();
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
I have a method in my service. If my deletion fails, how do I let the controller know it failed? Endpoint for reference:
/// <summary>
/// Deletes a reservation by ID.
/// </summary>
/// <param name="reservationId">ID of the reservation to delete.</param>
/// <returns>
/// 200 OK status code to confirm that the reservation has been deleted.
/// </returns>
[HttpDelete]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<IActionResult> DeleteReservation([FromQuery] int reservationId)
{
var existingReservation = await reservationStorage.Read(reservationId);

if (existingReservation == null)
{
return NotFound();
}

await reservationStorage.Delete(reservationId);
return Ok();
}
/// <summary>
/// Deletes a reservation by ID.
/// </summary>
/// <param name="reservationId">ID of the reservation to delete.</param>
/// <returns>
/// 200 OK status code to confirm that the reservation has been deleted.
/// </returns>
[HttpDelete]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<IActionResult> DeleteReservation([FromQuery] int reservationId)
{
var existingReservation = await reservationStorage.Read(reservationId);

if (existingReservation == null)
{
return NotFound();
}

await reservationStorage.Delete(reservationId);
return Ok();
}
55 replies