namespace WhatApi.Endpoints; public class UploadContentEndpoint(AppDatabase db) : BaseEndpoint { public record UploadContent(IFormFile File, string LatLong); [HttpPost("~/upload")] public async Task HandleAsync([FromForm] UploadContent request, CancellationToken ct = default) { if (string.IsNullOrWhiteSpace(Request.GetMultipartBoundary())) { return StatusCode(415, "Unsupported Media Type"); } var blobId = Guid.NewGuid(); var contentId = Guid.NewGuid(); var latitude = request.LatLong.Split(',')[0]; var longitude = request.LatLong.Split(',')[1]; var gf = NtsGeometryServices.Instance.CreateGeometryFactory(srid: Constants.Wgs84SpatialReferenceId); var point = gf.CreatePoint(new Coordinate(double.Parse(longitude), double.Parse(latitude))); var place = new Place() { ContentId = contentId, Location = point }; var content = new Content() { Id = contentId, Mime = request.File.ContentType, BlobId = blobId, Ip = GetIp() }; var path = Path.Combine(Directory.GetCurrentDirectory(), "files", blobId.ToString()); await using var writer = new FileStream(path, FileMode.CreateNew); await request.File.CopyToAsync(writer, ct); await db.Content.AddAsync(content, ct); await db.Places.AddAsync(place, ct); await db.SaveChangesAsync(ct); return Ok(contentId); } }