167 lines
4.8 KiB
C#
167 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.ComponentModel.DataAnnotations;
|
|
using System.Xml.Linq;
|
|
|
|
namespace Assignment3.Controllers
|
|
{
|
|
[Route("api/[controller]")]
|
|
[ApiController]
|
|
public class OrganizationsController : ControllerBase
|
|
{
|
|
private readonly Assignment3Context _context;
|
|
|
|
public OrganizationsController(Assignment3Context context)
|
|
{
|
|
_context = context;
|
|
}
|
|
|
|
// GET: api/Organizations
|
|
[HttpGet]
|
|
public async Task<ActionResult<IEnumerable<Organization>>> GetOrganization()
|
|
{
|
|
if (_context.Organization == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
return await _context.Organization.ToListAsync();
|
|
}
|
|
|
|
// GET: api/Organizations/5
|
|
[HttpGet("{id}")]
|
|
public async Task<ActionResult<Organization>> GetOrganization(Guid id)
|
|
{
|
|
if (_context.Organization == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
var organization = await _context.Organization.FindAsync(id);
|
|
|
|
if (organization == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
|
|
return organization;
|
|
}
|
|
|
|
// PUT: api/Organizations/5
|
|
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
|
|
[HttpPut("{id}")]
|
|
public async Task<IActionResult> PutOrganization(Guid id, Organization organization)
|
|
{
|
|
if (id != organization.Id)
|
|
{
|
|
return BadRequest();
|
|
}
|
|
|
|
_context.Entry(organization).State = EntityState.Modified;
|
|
|
|
try
|
|
{
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
catch (DbUpdateConcurrencyException)
|
|
{
|
|
if (!OrganizationExists(id))
|
|
{
|
|
return NotFound();
|
|
}
|
|
else
|
|
{
|
|
throw;
|
|
}
|
|
}
|
|
|
|
return NoContent();
|
|
}
|
|
|
|
// POST: api/Organizations
|
|
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
|
|
[HttpPost]
|
|
public async Task<ActionResult<Organization>> PostOrganization(Organization organization)
|
|
{
|
|
var validationResults = new List<ValidationResult>();
|
|
bool isValid = Validator.TryValidateObject(organization, new ValidationContext(organization), validationResults, true);
|
|
|
|
if (_context.Organization == null)
|
|
{
|
|
return Problem("Entity set 'Assignment3Context.Organization' is null.");
|
|
}
|
|
_context.Organization.Add(organization);
|
|
await _context.SaveChangesAsync();
|
|
|
|
return CreatedAtAction("GetOrganization", new { id = organization.Id }, organization);
|
|
}
|
|
|
|
// DELETE: api/Organizations/5
|
|
[HttpDelete("{id}")]
|
|
public async Task<IActionResult> DeleteOrganization(Guid id)
|
|
{
|
|
if (_context.Organization == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
var organization = await _context.Organization.FindAsync(id);
|
|
if (organization == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
|
|
_context.Organization.Remove(organization);
|
|
await _context.SaveChangesAsync();
|
|
|
|
return NoContent();
|
|
}
|
|
|
|
[HttpGet("name={value}")]
|
|
public async Task<ActionResult<Organization>> GetOrganizationName(string name)
|
|
{
|
|
if (_context.Organization == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
var organization = await _context.Organization.FirstOrDefaultAsync(i => i.Name == name);
|
|
|
|
if (organization == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
|
|
return organization;
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpGet("type={value}")]
|
|
public async Task<ActionResult<Organization>> GetOrganizationType(string type)
|
|
{
|
|
if (_context.Organization == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
var organization = await _context.Organization.FirstOrDefaultAsync(i => i.Type == type); ;
|
|
|
|
if (organization == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
|
|
return organization;
|
|
}
|
|
|
|
private bool OrganizationExists(Guid id)
|
|
{
|
|
return (_context.Organization?.Any(e => e.Id == id)).GetValueOrDefault();
|
|
}
|
|
}
|
|
}
|