/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: MIT-0
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this
* software and associated documentation files (the "Software"), to deal in the Software
* without restriction, including without limitation the rights to use, copy, modify,
* merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using InventoryService.Data;
using InventoryService.Interface;
using InventoryService.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
namespace InventoryService.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class UnicornController : ControllerBase
{
private readonly IUnicornService unicornService;
private readonly IRekognitionService rekognitionService;
private readonly ISNSService snsService;
public UnicornController(IUnicornService unicornService, IRekognitionService rekognitionService, ISNSService snsService)
{
this.unicornService = unicornService;
this.rekognitionService = rekognitionService;
this.snsService = snsService;
}
///
/// GET: api/Unicorn
///
/// List of Unicorn entities.
[HttpGet]
public ActionResult> GetUnicorns()
{
return this.unicornService.GetUnicorns();
}
///
/// GET: api/Unicorn/1d6d0345-b3e5-4e0f-87a3-0a98b9a17222
///
/// Unicorn Id to fetch a unicorn.
/// Unicorn entity.
[HttpGet("{id}")]
public async Task> GetUnicorn(Guid id)
{
var unicorn = await this.unicornService.GetUnicornAsync(id);
if (unicorn == null)
{
return this.NotFound();
}
return unicorn;
}
///
/// PUT: api/Unicorn/1d6d0345-b3e5-4e0f-87a3-0a98b9a17222
///
/// Unicorn Id to update.
/// Modified unicorn entity.
/// No content with status code 204.
[HttpPut("{id}")]
public async Task UpdateUnicorn(Guid id, Unicorn unicorn)
{
if (id != unicorn.unicorn_id)
{
return this.BadRequest();
}
var status = await this.unicornService.FindUnicornUpdateAsync(id, unicorn);
if (status == null)
{
return this.NotFound();
}
return this.NoContent();
}
///
/// POST: api/Unicorn
///
/// New unicorn entity.
/// Reroute to new unicorn.
[HttpPost]
public async Task> CreateUnicorn(NewUploadedItem upload)
{
// Uncomment the code below for the AI Content Moderation extra credit lab
/*
var rekognitionResponse = this.rekognitionService.GetContentModerationLabels(upload.Image);
if (rekognitionResponse != null)
{
return this.BadRequest(rekognitionResponse);
}
*/
var createItemResponse = await this.unicornService.CreateItemAsync(upload);
if (createItemResponse == null)
{
return this.BadRequest();
}
return this.CreatedAtAction("GetUnicorn", new { id = upload.Unicorn.unicorn_id }, upload.Unicorn);
}
///
/// DELETE: api/Unicorn/1d6d0345-b3e5-4e0f-87a3-0a98b9a17222
///
/// Unicorn Id to delete.
/// Deleted unicorn entity.
[HttpDelete("{id}")]
public async Task> DeleteUnicorn(Guid id)
{
var jsonMessage = JsonConvert.SerializeObject(new
{
unicorn_id = id,
available = false,
});
try
{
await this.snsService.PublishMessageToSNSAsync(jsonMessage);
}
catch (Exception)
{
return this.StatusCode(StatusCodes.Status500InternalServerError);
}
var unicorn = await this.unicornService.FindUnicornDeleteAsync(id);
if (unicorn == null)
{
return this.NotFound();
}
return unicorn;
}
}
}