From 16635951d87a0ee5e8e4eca3d98f0ba00d627e81 Mon Sep 17 00:00:00 2001 From: Natalia Portillo Date: Sat, 11 Aug 2018 10:50:31 +0100 Subject: [PATCH] Add API controllers. --- .../Api/Controllers/CompaniesController.cs | 69 +++++++++++++++++++ .../Api/Controllers/MachinesController.cs | 69 +++++++++++++++++++ .../Areas/Api/Controllers/NewsController.cs | 69 +++++++++++++++++++ 3 files changed, 207 insertions(+) create mode 100644 cicm_web/Areas/Api/Controllers/CompaniesController.cs create mode 100644 cicm_web/Areas/Api/Controllers/MachinesController.cs create mode 100644 cicm_web/Areas/Api/Controllers/NewsController.cs diff --git a/cicm_web/Areas/Api/Controllers/CompaniesController.cs b/cicm_web/Areas/Api/Controllers/CompaniesController.cs new file mode 100644 index 00000000..83c1217d --- /dev/null +++ b/cicm_web/Areas/Api/Controllers/CompaniesController.cs @@ -0,0 +1,69 @@ +/****************************************************************************** +// Canary Islands Computer Museum Website +// ---------------------------------------------------------------------------- +// +// Filename : CompaniesController.cs +// Author(s) : Natalia Portillo +// +// --[ Description ] ---------------------------------------------------------- +// +// Companies api controller +// +// --[ License ] -------------------------------------------------------------- +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +// +// ---------------------------------------------------------------------------- +// Copyright © 2003-2018 Natalia Portillo +*******************************************************************************/ + +using System.Collections.Generic; +using System.Threading.Tasks; +using Cicm.Database.Models; +using Microsoft.AspNetCore.Mvc; + +namespace cicm_web.Areas.Api.Controllers +{ + [Route("api/[controller]")] + [ApiController] + public class CompaniesController : ControllerBase + { + readonly cicmContext _context; + + public CompaniesController(cicmContext context) + { + _context = context; + } + + // GET: api/Companies + [HttpGet] + public IEnumerable GetCompanies() + { + return _context.Companies; + } + + // GET: api/Companies/5 + [HttpGet("{id}")] + public async Task GetCompany([FromRoute] int id) + { + if(!ModelState.IsValid) return BadRequest(ModelState); + + Company company = await _context.Companies.FindAsync(id); + + if(company == null) return NotFound(); + + return Ok(company); + } + } +} \ No newline at end of file diff --git a/cicm_web/Areas/Api/Controllers/MachinesController.cs b/cicm_web/Areas/Api/Controllers/MachinesController.cs new file mode 100644 index 00000000..e5b89193 --- /dev/null +++ b/cicm_web/Areas/Api/Controllers/MachinesController.cs @@ -0,0 +1,69 @@ +/****************************************************************************** +// Canary Islands Computer Museum Website +// ---------------------------------------------------------------------------- +// +// Filename : MachinesController.cs +// Author(s) : Natalia Portillo +// +// --[ Description ] ---------------------------------------------------------- +// +// Machines api controller +// +// --[ License ] -------------------------------------------------------------- +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +// +// ---------------------------------------------------------------------------- +// Copyright © 2003-2018 Natalia Portillo +*******************************************************************************/ + +using System.Collections.Generic; +using System.Threading.Tasks; +using Cicm.Database.Models; +using Microsoft.AspNetCore.Mvc; + +namespace cicm_web.Areas.Api.Controllers +{ + [Route("api/[controller]")] + [ApiController] + public class MachinesController : ControllerBase + { + readonly cicmContext _context; + + public MachinesController(cicmContext context) + { + _context = context; + } + + // GET: api/Machines + [HttpGet] + public IEnumerable GetMachines() + { + return _context.Machines; + } + + // GET: api/Machines/5 + [HttpGet("{id}")] + public async Task GetMachine([FromRoute] int id) + { + if(!ModelState.IsValid) return BadRequest(ModelState); + + Machine machine = await _context.Machines.FindAsync(id); + + if(machine == null) return NotFound(); + + return Ok(machine); + } + } +} \ No newline at end of file diff --git a/cicm_web/Areas/Api/Controllers/NewsController.cs b/cicm_web/Areas/Api/Controllers/NewsController.cs new file mode 100644 index 00000000..9481e872 --- /dev/null +++ b/cicm_web/Areas/Api/Controllers/NewsController.cs @@ -0,0 +1,69 @@ +/****************************************************************************** +// Canary Islands Computer Museum Website +// ---------------------------------------------------------------------------- +// +// Filename : NewsController.cs +// Author(s) : Natalia Portillo +// +// --[ Description ] ---------------------------------------------------------- +// +// News api controller +// +// --[ License ] -------------------------------------------------------------- +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +// +// ---------------------------------------------------------------------------- +// Copyright © 2003-2018 Natalia Portillo +*******************************************************************************/ + +using System.Collections.Generic; +using System.Threading.Tasks; +using Cicm.Database.Models; +using Microsoft.AspNetCore.Mvc; + +namespace cicm_web.Areas.Api.Controllers +{ + [Route("api/[controller]")] + [ApiController] + public class NewsController : ControllerBase + { + readonly cicmContext _context; + + public NewsController(cicmContext context) + { + _context = context; + } + + // GET: api/News + [HttpGet] + public IEnumerable GetNews() + { + return _context.News; + } + + // GET: api/News/5 + [HttpGet("{id}")] + public async Task GetNews([FromRoute] int id) + { + if(!ModelState.IsValid) return BadRequest(ModelState); + + News news = await _context.News.FindAsync(id); + + if(news == null) return NotFound(); + + return Ok(news); + } + } +} \ No newline at end of file