using System; using System.Linq; using System.Threading.Tasks; using Amazon.DynamoDBv2; using Amazon.DynamoDBv2.DataModel; using Amazon.DynamoDBv2.DocumentModel; using Amazon.S3; using ImageRecognition.API.Models; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Options; namespace ImageRecognition.API.Controllers { [ApiController] [Route("api/[controller]")] [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)] public class AlbumController : Controller { private AppOptions _appOptions; private readonly DynamoDBContext _ddbContext; private IAmazonS3 _s3Client; public AlbumController(IOptions appOptions, IAmazonDynamoDB dbClient, IAmazonS3 s3Client) { _appOptions = appOptions.Value; _s3Client = s3Client; _ddbContext = new DynamoDBContext(dbClient); } /// /// Get the list of albums the user has created. /// /// [HttpGet] [ProducesResponseType(200, Type = typeof(Album[]))] [ProducesResponseType(401)] [ProducesResponseType(403)] public async Task GetUserAlbums() { var userId = Utilities.GetUsername(HttpContext.User); var search = _ddbContext.QueryAsync(userId); var albums = await search.GetRemainingAsync().ConfigureAwait(false); return new JsonResult(albums); } /// /// Get the list of albums the user has created. /// /// If true then also include the galleries that have been marked as public. /// [HttpGet("{albumId}")] [ProducesResponseType(200, Type = typeof(Album))] [ProducesResponseType(401)] [ProducesResponseType(403)] public async Task GetAlbumById(string albumId) { var userId = Utilities.GetUsername(HttpContext.User); var albumQuery = _ddbContext.QueryAsync(userId, QueryOperator.Equal, new[] {albumId}); var album = await albumQuery.GetRemainingAsync().ConfigureAwait(false); return new JsonResult(album.FirstOrDefault()); } /// /// Create a new empty album. /// /// The name of the album. /// The album id to use for adding photos to the album. [HttpPut("{name}")] [ProducesResponseType(200, Type = typeof(CreateAlbumResult))] [ProducesResponseType(401)] [ProducesResponseType(403)] public async Task CreateAlbum(string name) { var userId = Utilities.GetUsername(HttpContext.User); var album = new Album { AlbumId = name + "-" + Guid.NewGuid(), Name = name, UserId = userId, CreateDate = DateTime.UtcNow, UpdatedDate = DateTime.UtcNow }; await _ddbContext.SaveAsync(album).ConfigureAwait(false); return new JsonResult(new CreateAlbumResult {AlbumId = album.AlbumId}); } /// /// Delete a album. /// /// The id of the album to delete. /// [HttpDelete("{albumId}")] [ProducesResponseType(200)] [ProducesResponseType(401)] [ProducesResponseType(403)] public async Task DeleteAlbum(string albumId) { var userId = Utilities.GetUsername(HttpContext.User); await _ddbContext.DeleteAsync(userId, albumId); return Ok(); } private class CreateAlbumResult { public string AlbumId { get; set; } } } }