|
| 1 | +/* |
| 2 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 3 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 4 | + * file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 5 | + * |
| 6 | + * Copyright © Vincent Bengtsson & Contributors 2022-2024 |
| 7 | + * https://github.com/Visual-Vincent/GuiStack |
| 8 | + */ |
| 9 | + |
| 10 | +using System; |
| 11 | +using System.Net; |
| 12 | +using System.Threading.Tasks; |
| 13 | +using Amazon.DynamoDBv2; |
| 14 | +using GuiStack.Extensions; |
| 15 | +using GuiStack.Models; |
| 16 | +using GuiStack.Repositories; |
| 17 | +using Microsoft.AspNetCore.Mvc; |
| 18 | + |
| 19 | +namespace GuiStack.Controllers.DynamoDB |
| 20 | +{ |
| 21 | + [ApiController] |
| 22 | + [Route("api/" + nameof(DynamoDB) + "/[controller]")] |
| 23 | + public class TablesController : Controller |
| 24 | + { |
| 25 | + private IDynamoDBRepository dynamodbRepository; |
| 26 | + |
| 27 | + public TablesController(IDynamoDBRepository dynamodbRepository) |
| 28 | + { |
| 29 | + this.dynamodbRepository = dynamodbRepository; |
| 30 | + } |
| 31 | + |
| 32 | + private ActionResult HandleException(Exception ex) |
| 33 | + { |
| 34 | + if(ex == null) |
| 35 | + throw new ArgumentNullException(nameof(ex)); |
| 36 | + |
| 37 | + if(ex is AmazonDynamoDBException dynamodbEx) |
| 38 | + { |
| 39 | + if(dynamodbEx.StatusCode == HttpStatusCode.NotFound) |
| 40 | + return StatusCode((int)dynamodbEx.StatusCode, new { error = dynamodbEx.Message }); |
| 41 | + |
| 42 | + Console.Error.WriteLine(dynamodbEx); |
| 43 | + return StatusCode((int)HttpStatusCode.InternalServerError, new { error = ex.Message }); |
| 44 | + } |
| 45 | + |
| 46 | + Console.Error.WriteLine(ex); |
| 47 | + return StatusCode((int)HttpStatusCode.InternalServerError); |
| 48 | + } |
| 49 | + |
| 50 | + [HttpPost] |
| 51 | + [Consumes("application/json")] |
| 52 | + public async Task<ActionResult> CreateTable([FromBody] DynamoDBCreateTableModel model) |
| 53 | + { |
| 54 | + if(string.IsNullOrWhiteSpace(model.TableName) || model.PartitionKey == null || string.IsNullOrEmpty(model.PartitionKey.Name)) |
| 55 | + return StatusCode((int)HttpStatusCode.BadRequest); |
| 56 | + |
| 57 | + try |
| 58 | + { |
| 59 | + await dynamodbRepository.CreateTableAsync(model); |
| 60 | + return Ok(); |
| 61 | + } |
| 62 | + catch(Exception ex) |
| 63 | + { |
| 64 | + return HandleException(ex); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + [HttpDelete("{tableName}")] |
| 69 | + public async Task<ActionResult> DeleteTable([FromRoute] string tableName) |
| 70 | + { |
| 71 | + if(string.IsNullOrWhiteSpace(tableName)) |
| 72 | + return StatusCode((int)HttpStatusCode.BadRequest); |
| 73 | + |
| 74 | + tableName = tableName.DecodeRouteParameter(); |
| 75 | + |
| 76 | + try |
| 77 | + { |
| 78 | + await dynamodbRepository.DeleteTableAsync(tableName); |
| 79 | + return Ok(); |
| 80 | + } |
| 81 | + catch(Exception ex) |
| 82 | + { |
| 83 | + return HandleException(ex); |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | +} |
0 commit comments