using System; using System.Collections.Generic; using System.Threading.Tasks; using LCU.API.Collections.RepoApi.Configuration; using LCU.API.Collections.RepoApi.Exceptions; using LCU.API.Collections.RepoApi.Extensions; using LCU.API.Collections.RepoApi.Services; using LCU.Shared.Collections.RepoApiLib.Models; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.FeatureManagement.Mvc; using Serilog; namespace LCU.API.Collections.RepoApi.Controllers { [Route("api/[controller]")] [ApiController] [FeatureGate(FeatureFlags.EnableControllers)] public class SavedLocationController : RepoControllerBase { private readonly ISavedLocationService _savedLocationService; public SavedLocationController( ISavedLocationService savedLocationService, IHttpContextAccessor httpContextAccessor) : base(httpContextAccessor) { _savedLocationService = savedLocationService; } // GET: api/SavedLocation [HttpGet] [ProducesResponseType(typeof(IEnumerable), 200)] [ProducesResponseType(500)] public async Task Get() { try { var savedLocations = await _savedLocationService.ListAllAsync(); return Ok(savedLocations); } catch (Exception ex) { Log.Error(ex, $"Failure in {GetType().Name} at {GetInvokerAsyncMethodName()}"); return StatusCode(StatusCodes.Status500InternalServerError); } } // GET: api/SavedLocation/5 [HttpGet("{id}", Name = "GetSavedLocationById")] [ProducesResponseType(typeof(SavedLocation), 200)] [ProducesResponseType(typeof(string), 400)] [ProducesResponseType(500)] public async Task GetById(int id) { try { if (id <= 0) { return BadRequest("Invalid ID"); } var savedLocation = await _savedLocationService.GetAsync(id); return Ok(savedLocation); } catch (NotFoundException nfe) { return NotFound(nfe.Message); } catch (Exception ex) { Log.Error(ex, $"Failure in {GetType().Name} at {GetInvokerAsyncMethodName()}"); return StatusCode(StatusCodes.Status500InternalServerError); } } // POST: api/SavedLocation [HttpPost] [ProducesResponseType(typeof(SavedLocation), 200)] [ProducesResponseType(typeof(List), 400)] [ProducesResponseType(500)] public async Task Post([FromBody] SavedLocation savedLocation) { try { if (!ModelState.IsValid) { return BadRequest(ModelState.GetErrorMessages()); } var newSavedLocation = await _savedLocationService.SaveAsync(savedLocation); return Ok(newSavedLocation); } catch (Exception ex) { Log.Error(ex, $"Failure in {GetType().Name} at {GetInvokerAsyncMethodName()}"); return StatusCode(StatusCodes.Status500InternalServerError); } } // PUT: api/SavedLocation/5 [HttpPut("{id}")] [ProducesResponseType(typeof(SavedLocation), 200)] [ProducesResponseType(typeof(List), 400)] [ProducesResponseType(500)] public async Task Put(int id, [FromBody] SavedLocation savedLocation) { try { if (!ModelState.IsValid) { return BadRequest(ModelState.GetErrorMessages()); } if (id <= 0 || id != savedLocation.ID) { return BadRequest("Invalid ID"); } var updatedSavedLocation = await _savedLocationService.UpdateAsync(savedLocation); return Ok(updatedSavedLocation); } catch (NotFoundException nfe) { return NotFound(nfe.Message); } catch (Exception ex) { Log.Error(ex, $"Failure in {GetType().Name} at {GetInvokerAsyncMethodName()}"); return StatusCode(StatusCodes.Status500InternalServerError); } } // DELETE: api/ApiWithActions/5 [HttpDelete("{id}")] [ProducesResponseType(typeof(SavedLocation), 200)] [ProducesResponseType(typeof(string), 400)] [ProducesResponseType(500)] public async Task Delete(int id) { try { if (id <= 0) { return BadRequest("Invalid ID"); } var result = await _savedLocationService.DeleteAsync(id); return Ok(result); } catch (NotFoundException nfe) { return NotFound(nfe.Message); } catch (Exception ex) { Log.Error(ex, $"Failure in {GetType().Name} at {GetInvokerAsyncMethodName()}"); return StatusCode(StatusCodes.Status500InternalServerError); } } } }