diff options
| author | ivarlovlie <git@ivarlovlie.no> | 2022-06-01 21:13:43 +0200 |
|---|---|---|
| committer | ivarlovlie <git@ivarlovlie.no> | 2022-06-01 21:13:43 +0200 |
| commit | 9383a2fb09ffb60cfe63683106945bd688affa59 (patch) | |
| tree | 65b3f4b48841583e355887db5de5a16e7005fc87 /src/Controllers/DocumentsController.cs | |
| download | vinjesvingenhandel.no-9383a2fb09ffb60cfe63683106945bd688affa59.tar.xz vinjesvingenhandel.no-9383a2fb09ffb60cfe63683106945bd688affa59.zip | |
feat: Initial commit after clean slate
Diffstat (limited to 'src/Controllers/DocumentsController.cs')
| -rw-r--r-- | src/Controllers/DocumentsController.cs | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/src/Controllers/DocumentsController.cs b/src/Controllers/DocumentsController.cs new file mode 100644 index 0000000..b2a6122 --- /dev/null +++ b/src/Controllers/DocumentsController.cs @@ -0,0 +1,76 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using IOL.Helpers; +using Microsoft.AspNetCore.Mvc; +using VSH.Data; +using VSH.Data.Database; +using VSH.Data.Enums; +using VSH.Data.Static; +using VSH.Utilities; + +namespace VSH.Controllers; + +public class DocumentsController : MainControllerBase +{ + private readonly MainDbContext _context; + + public DocumentsController(MainDbContext context) { + _context = context; + } + + [HttpGet("{type}")] + public ActionResult GetDocument(DocumentType type) { + var document = _context.Documents.SingleOrDefault(d => d.Type == type); + return Ok(document?.Content); + } + + [HttpPost("{type}")] + public ActionResult UpdateDocument([FromRoute] DocumentType type, [FromForm] string content) { + var document = _context.Documents.SingleOrDefault(d => d.Type == type); + if (document == default) { + var newDocument = new Document { + Content = content, + Type = type, + Created = DateTime.UtcNow + }; + _context.Documents.Add(newDocument); + _context.SaveChanges(); + return Ok(newDocument.Content); + } + + document.Content = content; + document.Updated = DateTime.UtcNow; + _context.SaveChanges(); + return Ok(document.Content); + } + + [HttpPost("upload-images")] + public ActionResult UploadImages() { + var files = Request.Form.Files; + if (files.Count == 0) + return BadRequest(); + var filePaths = new List<FileInfo>(); + var fileNames = new List<string>(); + foreach (var file in files) { + var fileName = RandomString.Generate(4, + true) + + "_" + + file.FileName; + var filePath = Path.Combine(AppPaths.DocumentImages.HostPath, + fileName); + using var writeStream = new FileStream(filePath, + FileMode.CreateNew); + file.CopyTo(writeStream); + filePaths.Add(new FileInfo(filePath)); + fileNames.Add(fileName); + } + + foreach (var filePath in filePaths) { + ImageFunctions.EnsureNormalOrLessImageResolution(filePath); + } + + return Ok(fileNames); + } +}
\ No newline at end of file |
