A3-Dot_Net/Assignment3/Controllers/PatientsController.cs
2025-03-23 00:12:27 -04:00

196 lines
4.8 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Assignment3.Data;
using Assignment3.Models;
using System.Xml.Linq;
namespace Assignment3.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class PatientsController : ControllerBase
{
private readonly Assignment3Context _context;
public PatientsController(Assignment3Context context)
{
_context = context;
}
// GET: api/Patients
[HttpGet]
public async Task<ActionResult<IEnumerable<Patient>>> GetPatient()
{
if (_context.Patient == null)
{
return NotFound();
}
return await _context.Patient.ToListAsync();
}
// GET: api/Patients/5
[HttpGet("{id}")]
public async Task<ActionResult<Patient>> GetPatient(Guid id)
{
if (_context.Patient == null)
{
return NotFound();
}
var patient = await _context.Patient.FindAsync(id);
if (patient == null)
{
return NotFound();
}
return patient;
}
// PUT: api/Patients/5
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
[HttpPut("{id}")]
public async Task<IActionResult> PutPatient(Guid id, Patient patient)
{
if (id != patient.Id)
{
return BadRequest();
}
_context.Entry(patient).State = EntityState.Modified;
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!PatientExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return NoContent();
}
// POST: api/Patients
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
[HttpPost]
public async Task<ActionResult<Patient>> PostPatient(Patient patient)
{
if (_context.Patient == null)
{
return Problem("Entity set 'Assignment3Context.Patient' is null.");
}
_context.Patient.Add(patient);
await _context.SaveChangesAsync();
return CreatedAtAction("GetPatient", new { id = patient.Id }, patient);
}
// DELETE: api/Patients/5
[HttpDelete("{id}")]
public async Task<IActionResult> DeletePatient(Guid id)
{
if (_context.Patient == null)
{
return NotFound();
}
var patient = await _context.Patient.FindAsync(id);
if (patient == null)
{
return NotFound();
}
_context.Patient.Remove(patient);
await _context.SaveChangesAsync();
return NoContent();
}
[HttpGet("firstName={value}")]
public async Task<ActionResult<Patient>> GetPatientFirstName(string firstName)
{
if (_context.Patient == null)
{
return NotFound();
}
var patient = await _context.Patient.FirstOrDefaultAsync(i => i.FirstName == firstName);
if (patient == null)
{
return NotFound();
}
return patient;
}
[HttpGet("lastName={value}")]
public async Task<ActionResult<Patient>> GetPatientLastName(string lastname)
{
if (_context.Patient == null)
{
return NotFound();
}
var patient = await _context.Patient.FirstOrDefaultAsync(i => i.LastName == lastname);
if (patient == null)
{
return NotFound();
}
return patient;
}
[HttpGet("dateOfBirth={value}")]
public async Task<ActionResult<Patient>> GetPatientdateOfBirth(DateTimeOffset date)
{
if (_context.Patient == null)
{
return NotFound();
}
var patient = await _context.Patient.FirstOrDefaultAsync(i => i.DateOfBirth == date);
if (patient == null)
{
return NotFound();
}
return patient;
}
private bool PatientExists(Guid id)
{
return (_context.Patient?.Any(e => e.Id == id)).GetValueOrDefault();
}
}
}