Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions AirAware/Controllers/ReadingController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
using AirAware.Data;
using AirAware.Models;
using AirAware.ViewModels;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

namespace AirAware.Controllers;

[ApiController]
[Route("api/v1")]
public class ReadingController: ControllerBase
{
[HttpGet]
[Route("readings")]
public async Task<IActionResult> GetAsync([FromServices] AppDbContext context)
{
var readings = await context
.Readings
.AsNoTracking()
.ToListAsync();
Comment thread
joaoferreira-dev marked this conversation as resolved.
return Ok(readings);
}

[HttpGet]
[Route("readings/{id}")]
public async Task<IActionResult> GetByIdAsync(
[FromServices] AppDbContext context,
[FromRoute] Guid id
)
{
var reading = await context
.Readings
.AsNoTracking()
.FirstOrDefaultAsync(r => r.Id == id);

return reading == null
? NotFound()
: Ok(reading);
}

[HttpPost("readings")]
public async Task<IActionResult> PostAsync(
[FromServices] AppDbContext context,
[FromBody] CreateReadingViewModel model
)
{
if (!ModelState.IsValid)
return BadRequest("Invalid data provided.");

var station = await context
.Stations
.AsNoTracking()
.FirstOrDefaultAsync(s => s.Id == model.StationId);

if (station == null)
return BadRequest("Station with the provided ID does not exist.");

var reading = new Reading
{
StationId = model.StationId,
Pm2_5 = model.Pm2_5,
Pm10 = model.Pm10,
RawPayload = model.RawPayload
};

try
{
await context.Readings.AddAsync(reading);
await context.SaveChangesAsync();
return Created($"api/v1/readings/{reading.Id}", reading);
}
catch (Exception)
{
return StatusCode(StatusCodes.Status500InternalServerError);
}
Comment thread
joaoferreira-dev marked this conversation as resolved.
}
}
2 changes: 1 addition & 1 deletion AirAware/Controllers/StationController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ [FromBody] CreateStationViewModel model
{
await context.Stations.AddAsync(station);
await context.SaveChangesAsync();
return Created($"v1/stations/{station.Id}", station);
return Created($"api/v1/stations/{station.Id}", station);
}
catch (Exception)
{
Expand Down
1 change: 1 addition & 0 deletions AirAware/Data/AppDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace AirAware.Data;
public class AppDbContext: DbContext
{
public DbSet<Station> Stations { get; set; }
public DbSet<Reading> Readings { get; set; }

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) =>
optionsBuilder.UseSqlite("DataSource=app.db;Cache=Shared");
Comment on lines 8 to 12
Copy link

Copilot AI Feb 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR adds a new Readings DbSet/entity, but the repo appears to have no EF Core migrations and no startup call to EnsureCreated()/Migrate(). Without schema initialization, hitting the new endpoints will fail at runtime with “no such table: Readings”. Add a migration (and document/apply it) or ensure the database is created/migrated on startup.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot open a new pull request to apply changes based on this feedback

Expand Down
15 changes: 15 additions & 0 deletions AirAware/Models/Reading.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Text.Json.Serialization;

namespace AirAware.Models;

public class Reading
{
public Guid Id { get; set; } = Guid.NewGuid();
public Guid StationId { get; set; }
[JsonIgnore]
public Station Station { get; set; } = null!;
public double Pm2_5 { get; set; }
public double Pm10 { get; set; }
public string? RawPayload { get; set; }
public DateTime CreatedAt { get; set; } = DateTime.Now;
Comment thread
joaoferreira-dev marked this conversation as resolved.
}
14 changes: 14 additions & 0 deletions AirAware/ViewModels/CreateReadingViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.ComponentModel.DataAnnotations;

namespace AirAware.ViewModels;

public class CreateReadingViewModel
{
[Required]
public Guid StationId { get; set; }
[Required]
public double Pm2_5 { get; set; }
[Required]
public double Pm10 { get; set; }
Comment thread
joaoferreira-dev marked this conversation as resolved.
public string? RawPayload { get; set; }
}