From e39a826718441946a48e7e8c28a84933c2882d8a Mon Sep 17 00:00:00 2001 From: Ognjen Cirkovic Date: Thu, 3 Nov 2022 21:23:05 +0100 Subject: Napravljena kolekcija za fajlove. Omoguceno uplodovanje fajlova kada se dodaje objava. Dodat role za User-a u jwt-ju. --- Backend/Api/Api/.gitignore | 1 + Backend/Api/Api/Api.csproj | 4 ++ Backend/Api/Api/Controllers/PostController.cs | 2 +- Backend/Api/Api/Database/DatabaseConnection.cs | 1 + Backend/Api/Api/Interfaces/IDatabaseConnection.cs | 1 + Backend/Api/Api/Models/Post.cs | 5 ++- Backend/Api/Api/Program.cs | 5 ++- Backend/Api/Api/Services/FileService.cs | 28 +++++++++++++ Backend/Api/Api/Services/IFileService.cs | 8 ++++ Backend/Api/Api/Services/JwtService.cs | 3 +- Backend/Api/Api/Services/PostService.cs | 49 +++++++++++++++++++++-- Backend/Api/Api/appsettings.json | 13 +++--- 12 files changed, 104 insertions(+), 16 deletions(-) create mode 100644 Backend/Api/Api/.gitignore create mode 100644 Backend/Api/Api/Services/FileService.cs create mode 100644 Backend/Api/Api/Services/IFileService.cs (limited to 'Backend') diff --git a/Backend/Api/Api/.gitignore b/Backend/Api/Api/.gitignore new file mode 100644 index 0000000..f4b4ba8 --- /dev/null +++ b/Backend/Api/Api/.gitignore @@ -0,0 +1 @@ +Files/* \ No newline at end of file diff --git a/Backend/Api/Api/Api.csproj b/Backend/Api/Api/Api.csproj index 93e31b7..b09c2fd 100644 --- a/Backend/Api/Api/Api.csproj +++ b/Backend/Api/Api/Api.csproj @@ -15,4 +15,8 @@ + + + + diff --git a/Backend/Api/Api/Controllers/PostController.cs b/Backend/Api/Api/Controllers/PostController.cs index 8db68a8..31dbeef 100644 --- a/Backend/Api/Api/Controllers/PostController.cs +++ b/Backend/Api/Api/Controllers/PostController.cs @@ -20,7 +20,7 @@ namespace Api.Controllers [HttpPost("add")] [Authorize(Roles ="User")] - public async Task> addPost([FromBody] PostReceive post) + public async Task> addPost([FromForm]PostReceive post) { var res = await _postService.addPost(post); if (res != null) diff --git a/Backend/Api/Api/Database/DatabaseConnection.cs b/Backend/Api/Api/Database/DatabaseConnection.cs index c2fea05..24b2b08 100644 --- a/Backend/Api/Api/Database/DatabaseConnection.cs +++ b/Backend/Api/Api/Database/DatabaseConnection.cs @@ -8,5 +8,6 @@ namespace Api.Database public string DatabaseName { get; set; } = String.Empty; public string UserCollectionName { get; set; } = String.Empty; public string PostCollectionName { get; set; } = String.Empty; + public string FileCollectionName { get; set; } = String.Empty; } } diff --git a/Backend/Api/Api/Interfaces/IDatabaseConnection.cs b/Backend/Api/Api/Interfaces/IDatabaseConnection.cs index e3fc60c..744461b 100644 --- a/Backend/Api/Api/Interfaces/IDatabaseConnection.cs +++ b/Backend/Api/Api/Interfaces/IDatabaseConnection.cs @@ -6,5 +6,6 @@ string DatabaseName { get; set; } string UserCollectionName { get; set; } string PostCollectionName { get; set; } + string FileCollectionName { get; set; } } } diff --git a/Backend/Api/Api/Models/Post.cs b/Backend/Api/Api/Models/Post.cs index 456fcea..ee84e0f 100644 --- a/Backend/Api/Api/Models/Post.cs +++ b/Backend/Api/Api/Models/Post.cs @@ -1,5 +1,6 @@ using MongoDB.Bson.Serialization.Attributes; using MongoDB.Bson; +using System.ComponentModel.DataAnnotations; namespace Api.Models { @@ -9,7 +10,7 @@ namespace Api.Models [BsonRepresentation(BsonType.ObjectId)] public string _id { get; set; } public string ownerId { get; set; } - public Location location { get; set; } + public string locationId { get; set; } public string description { get; set; } public List views { get; set; } public List reports { get; set; } @@ -22,7 +23,7 @@ namespace Api.Models [BsonId] [BsonRepresentation(BsonType.ObjectId)] public string _id { get; set; } - public Location location { get; set; } + public string locationId { get; set; } public string description { get; set; } public List images { get; set; } diff --git a/Backend/Api/Api/Program.cs b/Backend/Api/Api/Program.cs index 9b80f3c..8dee088 100644 --- a/Backend/Api/Api/Program.cs +++ b/Backend/Api/Api/Program.cs @@ -23,6 +23,7 @@ builder.Services.AddSingleton(s => builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); +builder.Services.AddScoped(); builder.Services.AddHttpContextAccessor(); @@ -70,11 +71,11 @@ if (app.Environment.IsDevelopment()) app.UseSwaggerUI(); } -app.UseAuthorization(); + //Add Authentication app.UseAuthentication(); - +app.UseAuthorization(); app.MapControllers(); app.Run(); diff --git a/Backend/Api/Api/Services/FileService.cs b/Backend/Api/Api/Services/FileService.cs new file mode 100644 index 0000000..1937c10 --- /dev/null +++ b/Backend/Api/Api/Services/FileService.cs @@ -0,0 +1,28 @@ +using Api.Interfaces; +using Api.Models; +using MongoDB.Driver; +using File = Api.Models.File; + +namespace Api.Services +{ + public class FileService : IFileService + { + private readonly MongoClient _client; + private readonly IMongoCollection _files; + private readonly IHttpContextAccessor _httpContext; + public FileService(IDatabaseConnection settings, IMongoClient mongoClient) + { + var database = mongoClient.GetDatabase(settings.DatabaseName); + _files = database.GetCollection(settings.FileCollectionName); + } + public async Task add(File file) + { + await _files.InsertOneAsync(file); + return file; + } + public async Task getById(string id) + { + return await _files.Find(file => file._id == id).FirstOrDefaultAsync(); + } + } +} diff --git a/Backend/Api/Api/Services/IFileService.cs b/Backend/Api/Api/Services/IFileService.cs new file mode 100644 index 0000000..269e202 --- /dev/null +++ b/Backend/Api/Api/Services/IFileService.cs @@ -0,0 +1,8 @@ +namespace Api.Services +{ + public interface IFileService + { + Task add(Models.File file); + Task getById(string id); + } +} \ No newline at end of file diff --git a/Backend/Api/Api/Services/JwtService.cs b/Backend/Api/Api/Services/JwtService.cs index fbf5724..c199484 100644 --- a/Backend/Api/Api/Services/JwtService.cs +++ b/Backend/Api/Api/Services/JwtService.cs @@ -24,7 +24,8 @@ namespace Api.Services var key = Encoding.ASCII.GetBytes(_config.GetSection("AppSettings:JwtToken").Value); var tokenDescriptor = new SecurityTokenDescriptor { - Subject = new ClaimsIdentity(new[] { new Claim("id", user._id) }), + Subject = new ClaimsIdentity(new[] { new Claim("id", user._id), + new Claim("role","User")}), Expires = DateTime.UtcNow.AddDays(7), SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature) }; diff --git a/Backend/Api/Api/Services/PostService.cs b/Backend/Api/Api/Services/PostService.cs index 49c92cb..2f29366 100644 --- a/Backend/Api/Api/Services/PostService.cs +++ b/Backend/Api/Api/Services/PostService.cs @@ -10,11 +10,13 @@ namespace Api.Services private readonly MongoClient _client; private readonly IMongoCollection _posts; private readonly IHttpContextAccessor _httpContext; - public PostService(IDatabaseConnection settings, IMongoClient mongoClient, IHttpContextAccessor httpContext) + private readonly IFileService _fileService; + public PostService(IDatabaseConnection settings, IMongoClient mongoClient, IHttpContextAccessor httpContext, IFileService fileService) { var database = mongoClient.GetDatabase(settings.DatabaseName); _posts = database.GetCollection(settings.PostCollectionName); _httpContext = httpContext; + _fileService = fileService; } public async Task addPost(PostReceive post) @@ -22,14 +24,53 @@ namespace Api.Services Post p = new Post(); p._id = ""; p.ownerId = _httpContext.HttpContext.User.FindFirstValue("id"); - p.location = post.location; + + p.locationId = post.locationId; p.description = post.description; p.views = new List(); p.reports = new List(); p.ratings = new List(); p.comments = new List(); - //add file - //add to database + p.images = new List(); + + var folderPath = Path.Combine(Directory.GetCurrentDirectory(), "Files", p.ownerId); + if (!Directory.Exists(folderPath)) + { + Directory.CreateDirectory(folderPath); + } + + foreach (var image in post.images) + { + var filename = image.FileName; + var ext=Path.GetExtension(filename).ToLowerInvariant(); + var name = Path.GetFileNameWithoutExtension(filename).ToLowerInvariant(); + var fullPath=Path.Combine(folderPath, name); + int i = 0; + while (System.IO.File.Exists(fullPath)) + { + i++; + fullPath=Path.Combine(folderPath, name+i.ToString()+ext); + } + using(var stream=new FileStream(fullPath, FileMode.Create)) + { + await image.CopyToAsync(stream); + } + var f = new Models.File(); + f.path = fullPath; + f._id = ""; + f=await _fileService.add(f); + p.images.Add(f); + + } + await _posts.InsertOneAsync(p); + + + + + + + + return postToPostSend(p); } diff --git a/Backend/Api/Api/appsettings.json b/Backend/Api/Api/appsettings.json index 2be2426..d506b33 100644 --- a/Backend/Api/Api/appsettings.json +++ b/Backend/Api/Api/appsettings.json @@ -11,14 +11,15 @@ } }, "AllowedHosts": "*", - "DatabaseSettings": { + "DatabaseSettings": { - "ConnectionString": "mongodb://127.0.0.1:27017/", - "DatabaseName": "Odyssey", - "UserCollectionName": "users", - "PostCollectionName": "posts" + "ConnectionString": "mongodb://127.0.0.1:27017/", + "DatabaseName": "Odyssey", + "UserCollectionName": "users", + "PostCollectionName": "posts", + "FileCollectionName": "files" - }, + }, "EmailCfg": { "Email": "oddyssey.brzodolokacije@gmail.com", "SmtpServer": "smtp.gmail.com", -- cgit v1.2.3