223 lines
5.8 KiB
C#
223 lines
5.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 ImmunizationsController : ControllerBase
|
|
{
|
|
private readonly Assignment3Context _context;
|
|
|
|
public ImmunizationsController(Assignment3Context context)
|
|
{
|
|
_context = context;
|
|
}
|
|
|
|
// GET: api/Immunizations
|
|
[HttpGet]
|
|
public async Task<ActionResult<IEnumerable<Immunization>>> GetImmunization()
|
|
{
|
|
if (_context.Immunization == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
return await _context.Immunization.ToListAsync();
|
|
}
|
|
|
|
// GET: api/Immunizations/5
|
|
[HttpGet("{id}")]
|
|
public async Task<ActionResult<Immunization>> GetImmunization(Guid id)
|
|
{
|
|
if (_context.Immunization == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
var immunization = await _context.Immunization.FindAsync(id);
|
|
|
|
if (immunization == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
|
|
return immunization;
|
|
}
|
|
|
|
// PUT: api/Immunizations/5
|
|
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
|
|
[HttpPut("{id}")]
|
|
public async Task<IActionResult> PutImmunization(Guid id, Immunization immunization)
|
|
{
|
|
if (id != immunization.Id)
|
|
{
|
|
return BadRequest();
|
|
}
|
|
|
|
_context.Entry(immunization).State = EntityState.Modified;
|
|
|
|
try
|
|
{
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
catch (DbUpdateConcurrencyException)
|
|
{
|
|
if (!ImmunizationExists(id))
|
|
{
|
|
return NotFound();
|
|
}
|
|
else
|
|
{
|
|
throw;
|
|
}
|
|
}
|
|
|
|
return NoContent();
|
|
}
|
|
|
|
// POST: api/Immunizations
|
|
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
|
|
[HttpPost]
|
|
public async Task<ActionResult<Immunization>> PostImmunization(Immunization immunization)
|
|
{
|
|
if (_context.Immunization == null)
|
|
{
|
|
return Problem("Entity set 'Assignment3Context.Immunization' is null.");
|
|
}
|
|
_context.Immunization.Add(immunization);
|
|
await _context.SaveChangesAsync();
|
|
|
|
return CreatedAtAction("GetImmunization", new { id = immunization.Id }, immunization);
|
|
}
|
|
|
|
// DELETE: api/Immunizations/5
|
|
[HttpDelete("{id}")]
|
|
public async Task<IActionResult> DeleteImmunization(Guid id)
|
|
{
|
|
if (_context.Immunization == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
var immunization = await _context.Immunization.FindAsync(id);
|
|
if (immunization == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
|
|
_context.Immunization.Remove(immunization);
|
|
await _context.SaveChangesAsync();
|
|
|
|
return NoContent();
|
|
}
|
|
|
|
private bool ImmunizationExists(Guid id)
|
|
{
|
|
return (_context.Immunization?.Any(e => e.Id == id)).GetValueOrDefault();
|
|
}
|
|
|
|
|
|
[HttpGet("creationTime={value}")]
|
|
public async Task<ActionResult<Immunization>> GetcreationTime(DateTimeOffset time)
|
|
{
|
|
if (_context.Immunization == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
|
|
var immunization = await _context.Immunization
|
|
.FirstOrDefaultAsync(i => i.CreationTime == time);
|
|
|
|
if (immunization == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
|
|
return immunization;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
//Fixed to search the no primary column
|
|
// also change url name to match function input of name
|
|
// does it need to return just one or many if it applies to many for searching
|
|
[HttpGet("officialName={name}")]
|
|
public async Task<ActionResult<Immunization>> GetofficialName(String name)
|
|
{
|
|
if (_context.Immunization == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
var immunizations = await _context.Immunization
|
|
.FirstOrDefaultAsync(i => i.OfficialName == name);
|
|
|
|
if (immunizations == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
|
|
return Ok(immunizations);
|
|
|
|
}
|
|
|
|
|
|
|
|
[HttpGet("tradeName={value}")]
|
|
public async Task<ActionResult<Immunization>> GettradeName(String name)
|
|
{
|
|
if (_context.Immunization == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
var immunization = await _context.Immunization
|
|
.FirstOrDefaultAsync(i => i.TradeName == name);
|
|
|
|
if (immunization == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
|
|
return immunization;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
[HttpGet("lotNumber={value}")]
|
|
public async Task<ActionResult<Immunization>> GetlotNumber(String number)
|
|
{
|
|
if (_context.Immunization == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
var immunization = await _context.Immunization
|
|
.FirstOrDefaultAsync(i => i.LotNumber == number);
|
|
|
|
if (immunization == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
|
|
return immunization;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
}
|