-
Notifications
You must be signed in to change notification settings - Fork 0
feat: adding Reading.cs model and Get, GetById and Post method #14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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(); | ||
| 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); | ||
| } | ||
|
joaoferreira-dev marked this conversation as resolved.
|
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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
|
||
|
|
||
| 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; | ||
|
joaoferreira-dev marked this conversation as resolved.
|
||
| } | ||
| 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; } | ||
|
joaoferreira-dev marked this conversation as resolved.
|
||
| public string? RawPayload { get; set; } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.