diff options
author | Ognjen Cirkovic <ciraboxkg@gmail.com> | 2022-11-03 21:23:05 +0100 |
---|---|---|
committer | Ognjen Cirkovic <ciraboxkg@gmail.com> | 2022-11-03 21:23:05 +0100 |
commit | e39a826718441946a48e7e8c28a84933c2882d8a (patch) | |
tree | c80333d228717691bf7d7fb786b0078d093bbba4 | |
parent | 718a4a30cad0205e00b7d4acdee58c1b7f8cdb08 (diff) |
Napravljena kolekcija za fajlove. Omoguceno uplodovanje fajlova kada se dodaje objava. Dodat role za User-a u jwt-ju.
-rw-r--r-- | Backend/Api/Api/.gitignore | 1 | ||||
-rw-r--r-- | Backend/Api/Api/Api.csproj | 4 | ||||
-rw-r--r-- | Backend/Api/Api/Controllers/PostController.cs | 2 | ||||
-rw-r--r-- | Backend/Api/Api/Database/DatabaseConnection.cs | 1 | ||||
-rw-r--r-- | Backend/Api/Api/Interfaces/IDatabaseConnection.cs | 1 | ||||
-rw-r--r-- | Backend/Api/Api/Models/Post.cs | 5 | ||||
-rw-r--r-- | Backend/Api/Api/Program.cs | 5 | ||||
-rw-r--r-- | Backend/Api/Api/Services/FileService.cs | 28 | ||||
-rw-r--r-- | Backend/Api/Api/Services/IFileService.cs | 8 | ||||
-rw-r--r-- | Backend/Api/Api/Services/JwtService.cs | 3 | ||||
-rw-r--r-- | Backend/Api/Api/Services/PostService.cs | 49 | ||||
-rw-r--r-- | Backend/Api/Api/appsettings.json | 13 |
12 files changed, 104 insertions, 16 deletions
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 @@ <PackageReference Include="MailKit" Version="3.4.2" /> </ItemGroup> + <ItemGroup> + <Folder Include="Files\" /> + </ItemGroup> + </Project> 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<ActionResult<PostSend>> addPost([FromBody] PostReceive post) + public async Task<ActionResult<PostSend>> 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<string> views { get; set; } public List<string> 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<IFormFile> 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<IMongoClient>(s => builder.Services.AddScoped<IUserService, UserService>(); builder.Services.AddScoped<IJwtService, JwtService>(); builder.Services.AddScoped<IPostService,PostService>(); +builder.Services.AddScoped<IFileService,FileService>(); 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<File> _files; + private readonly IHttpContextAccessor _httpContext; + public FileService(IDatabaseConnection settings, IMongoClient mongoClient) + { + var database = mongoClient.GetDatabase(settings.DatabaseName); + _files = database.GetCollection<File>(settings.FileCollectionName); + } + public async Task<File> add(File file) + { + await _files.InsertOneAsync(file); + return file; + } + public async Task<File> 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<Models.File> add(Models.File file); + Task<Models.File> 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<Post> _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<Post>(settings.PostCollectionName); _httpContext = httpContext; + _fileService = fileService; } public async Task<PostSend> 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<string>(); p.reports = new List<string>(); p.ratings = new List<Rating>(); p.comments = new List<Comment>(); - //add file - //add to database + p.images = new List<Models.File>(); + + 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", |