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

190 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 NuGet.Packaging.Signing;
namespace Assignment3.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class ProvidersController : ControllerBase
{
private readonly Assignment3Context _context;
public ProvidersController(Assignment3Context context)
{
_context = context;
}
// GET: api/Providers
[HttpGet]
public async Task<ActionResult<IEnumerable<Provider>>> GetProvider()
{
if (_context.Provider == null)
{
return NotFound();
}
return await _context.Provider.ToListAsync();
}
// GET: api/Providers/5
[HttpGet("{id}")]
public async Task<ActionResult<Provider>> GetProvider(Guid id)
{
if (_context.Provider == null)
{
return NotFound();
}
var provider = await _context.Provider.FindAsync(id);
if (provider == null)
{
return NotFound();
}
return provider;
}
// PUT: api/Providers/5
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
[HttpPut("{id}")]
public async Task<IActionResult> PutProvider(Guid id, Provider provider)
{
if (id != provider.Id)
{
return BadRequest();
}
_context.Entry(provider).State = EntityState.Modified;
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!ProviderExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return NoContent();
}
// POST: api/Providers
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
[HttpPost]
public async Task<ActionResult<Provider>> PostProvider(Provider provider)
{
if (_context.Provider == null)
{
return Problem("Entity set 'Assignment3Context.Provider' is null.");
}
_context.Provider.Add(provider);
await _context.SaveChangesAsync();
return CreatedAtAction("GetProvider", new { id = provider.Id }, provider);
}
// DELETE: api/Providers/5
[HttpDelete("{id}")]
public async Task<IActionResult> DeleteProvider(Guid id)
{
if (_context.Provider == null)
{
return NotFound();
}
var provider = await _context.Provider.FindAsync(id);
if (provider == null)
{
return NotFound();
}
_context.Provider.Remove(provider);
await _context.SaveChangesAsync();
return NoContent();
}
[HttpGet("firstName={value}")]
public async Task<ActionResult<Provider>> GetProviderFirstName(string firstName)
{
if (_context.Provider == null)
{
return NotFound();
}
var provider = await _context.Provider.FirstOrDefaultAsync(i => i.FirstName == firstName);
if (provider == null)
{
return NotFound();
}
return provider;
}
[HttpGet("lastName={value}")]
public async Task<ActionResult<Provider>> GetProviderLastName(string lastname)
{
if (_context.Provider == null)
{
return NotFound();
}
var provider = await _context.Provider.FirstOrDefaultAsync(i => i.LastName == lastname);
if (provider == null)
{
return NotFound();
}
return provider;
}
[HttpGet("licenseNumber={value}")]
public async Task<ActionResult<Provider>> GetProviderlicenseNumber(long number)
{
if (_context.Provider == null)
{
return NotFound();
}
var provider = await _context.Provider.FirstOrDefaultAsync(i => i.LicenseNumber == number);
if (provider == null)
{
return NotFound();
}
return provider;
}
private bool ProviderExists(Guid id)
{
return (_context.Provider?.Any(e => e.Id == id)).GetValueOrDefault();
}
}
}