diff options
Diffstat (limited to 'Backend')
-rw-r--r-- | Backend/Api/Api/Controllers/PostController.cs | 7 | ||||
-rw-r--r-- | Backend/Api/Api/Controllers/UserController.cs | 51 | ||||
-rw-r--r-- | Backend/Api/Api/Interfaces/IPostService.cs | 2 | ||||
-rw-r--r-- | Backend/Api/Api/Interfaces/IUserService.cs | 4 | ||||
-rw-r--r-- | Backend/Api/Api/Models/User.cs | 10 | ||||
-rw-r--r-- | Backend/Api/Api/Services/PostService.cs | 6 | ||||
-rw-r--r-- | Backend/Api/Api/Services/UserService.cs | 58 |
7 files changed, 129 insertions, 9 deletions
diff --git a/Backend/Api/Api/Controllers/PostController.cs b/Backend/Api/Api/Controllers/PostController.cs index 3faaa62..dc48c73 100644 --- a/Backend/Api/Api/Controllers/PostController.cs +++ b/Backend/Api/Api/Controllers/PostController.cs @@ -89,11 +89,12 @@ namespace Api.Controllers [HttpPost("posts/{id}/addcomment")] [Authorize(Roles = "User")] - public async Task<ActionResult> addComment([FromBody] CommentReceive cmnt,string id) + public async Task<ActionResult<Comment>> addComment([FromBody] CommentReceive cmnt,string id) { var userid = await _userService.UserIdFromJwt(); - if (await _postService.AddComment(cmnt,userid,id)) - return Ok(); + var c = await _postService.AddComment(cmnt, userid, id); + if (c != null) + return Ok(c); return BadRequest(); } diff --git a/Backend/Api/Api/Controllers/UserController.cs b/Backend/Api/Api/Controllers/UserController.cs new file mode 100644 index 0000000..4e0f0a3 --- /dev/null +++ b/Backend/Api/Api/Controllers/UserController.cs @@ -0,0 +1,51 @@ +using Api.Interfaces; +using Api.Models; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; + +namespace Api.Controllers +{ + [Route("api/user/")] + public class UserController : Controller + { + private readonly IUserService _userService; + private readonly IJwtService _jwtService; + public UserController(IUserService userService, IJwtService jwtService) + { + _userService = userService; + _jwtService = jwtService; + } + [HttpPost("profile/pfp")] + [Authorize(Roles = "User")] + public async Task<ActionResult<User>> setPfp([FromBody] IFormFile image) + { + if (image == null) + return BadRequest(); + var id = await _userService.UserIdFromJwt(); + if (id == null) + return Unauthorized(); + if(await _userService.AddOrChangePfp(id,image)) + return Ok(); + return BadRequest(); + } + [HttpGet("profile")] + [Authorize(Roles = "User")] + public async Task<ActionResult<UserSend>> SelfProfile() + { + var id = await _userService.UserIdFromJwt(); + var rez = await _userService.GetSelfUserData(id); + if (rez != null) + return Ok(rez); + return BadRequest(); + } + [HttpGet("{username}/profile")] + [Authorize(Roles = "User")] + public async Task<ActionResult<UserSend>> GetProfile(string username) + { + var rez = await _userService.GetUserData(username); + if (rez != null) + return Ok(rez); + return BadRequest(); + } + } +} diff --git a/Backend/Api/Api/Interfaces/IPostService.cs b/Backend/Api/Api/Interfaces/IPostService.cs index 0274b26..24a1e74 100644 --- a/Backend/Api/Api/Interfaces/IPostService.cs +++ b/Backend/Api/Api/Interfaces/IPostService.cs @@ -10,7 +10,7 @@ namespace Api.Interfaces Task<PostSend> postToPostSend(Post post); Task<Boolean> AddOrReplaceRating(RatingReceive rating, string userid); Task<Boolean> RemoveRating(string postid, string userid); - Task<Boolean> AddComment(CommentReceive cmnt, string userid, string postid); + Task<Comment> AddComment(CommentReceive cmnt, string userid, string postid); Task<List<CommentSend>> ListComments(string postid); Task<List<CommentSend>> CascadeComments(string parentid, Post p); Task<Boolean> DeleteComments(string postid, string cmntid,string userid); diff --git a/Backend/Api/Api/Interfaces/IUserService.cs b/Backend/Api/Api/Interfaces/IUserService.cs index db2eac1..313e909 100644 --- a/Backend/Api/Api/Interfaces/IUserService.cs +++ b/Backend/Api/Api/Interfaces/IUserService.cs @@ -23,6 +23,8 @@ namespace Api.Interfaces Task<Boolean> ResetPassword(ResetPass rp); Task<Boolean> CheckVerification(Login login); Task<Boolean> VerifyFromToken(string token); - + Task<Boolean> AddOrChangePfp(string userid, IFormFile image); + Task<UserSend> GetUserData(string username); + Task<UserSend> GetSelfUserData(string id); } } diff --git a/Backend/Api/Api/Models/User.cs b/Backend/Api/Api/Models/User.cs index a1fe149..b212ebb 100644 --- a/Backend/Api/Api/Models/User.cs +++ b/Backend/Api/Api/Models/User.cs @@ -15,6 +15,7 @@ namespace Api.Models public Boolean verified { get; set; } public String password { get; set; } public DateTime creationDate { get; set; } + public File? pfp { get; set; } } public class Login @@ -48,4 +49,13 @@ namespace Api.Models public String password { get; set; } public String kod { get; set; } } + public class UserSend + { + public String _id { get; set; } + public String name { get; set; } + public String username { get; set; } + public String email { get; set; } + public DateTime creationDate { get; set; } + public File? pfp { get; set; } + } } diff --git a/Backend/Api/Api/Services/PostService.cs b/Backend/Api/Api/Services/PostService.cs index a0b2941..321aebe 100644 --- a/Backend/Api/Api/Services/PostService.cs +++ b/Backend/Api/Api/Services/PostService.cs @@ -164,7 +164,7 @@ namespace Api.Services } return false; } - public async Task<Boolean> AddComment(CommentReceive cmnt,string userid,string postid) + public async Task<Comment> AddComment(CommentReceive cmnt,string userid,string postid) { Post p = await _posts.Find(post => post._id == postid).FirstOrDefaultAsync(); if (p != null) @@ -177,9 +177,9 @@ namespace Api.Services c._id = ObjectId.GenerateNewId().ToString(); p.comments.Add(c); await _posts.ReplaceOneAsync(x => x._id == postid, p); - return true; + return c; } - return false; + return null; } public async Task<List<CommentSend>> ListComments(string postid) diff --git a/Backend/Api/Api/Services/UserService.cs b/Backend/Api/Api/Services/UserService.cs index 5fd61f6..d8361cd 100644 --- a/Backend/Api/Api/Services/UserService.cs +++ b/Backend/Api/Api/Services/UserService.cs @@ -14,13 +14,15 @@ namespace Api.Services private readonly IMongoCollection<User> _users; private readonly IJwtService _jwtService; private IConfiguration _configuration; - public UserService(IDatabaseConnection settings, IMongoClient mongoClient, IJwtService jwtService, IHttpContextAccessor httpContextAccessor, IConfiguration configuration) + private readonly IFileService _fileService; + public UserService(IDatabaseConnection settings, IMongoClient mongoClient, IJwtService jwtService, IHttpContextAccessor httpContextAccessor, IConfiguration configuration, IFileService fileService) { var database = mongoClient.GetDatabase(settings.DatabaseName); _users = database.GetCollection<User>(settings.UserCollectionName); _jwtService = jwtService; this._httpContext = httpContextAccessor; this._configuration = configuration; + _fileService = fileService; } public async Task<int> createUser(User user) @@ -311,5 +313,59 @@ namespace Api.Services } return false; } + + public async Task<Boolean> AddOrChangePfp(string userid,IFormFile image) + { + var user = await _users.Find(x => x._id == userid).FirstOrDefaultAsync(); + if (user == null) + return false; + var folderPath = Path.Combine(Directory.GetCurrentDirectory(), "Files", userid); + if (!Directory.Exists(folderPath)) + Directory.CreateDirectory(folderPath); + var filename = image.FileName; + var ext = Path.GetExtension(filename).ToLowerInvariant(); + var name ="PFP - " + user._id; + var fullPath = Path.Combine(folderPath, name+ext); + if (System.IO.File.Exists(fullPath)) + System.IO.File.Delete(fullPath); + 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); + user.pfp = f; + await _users.ReplaceOneAsync(x => x._id == user._id, user); + return true; + } + + public async Task<UserSend> GetUserData(string username) + { + var user = await _users.Find(x => x.username == username).FirstOrDefaultAsync(); + if(user == null) + return null; + var tosend = new UserSend(); + tosend.pfp = user.pfp; + tosend.username = user.username; + tosend._id= user._id; + tosend.creationDate = user.creationDate; + tosend.email=""; + return tosend; + } + public async Task<UserSend> GetSelfUserData(string id) + { + var user = await _users.Find(x => x._id == id).FirstOrDefaultAsync(); + if (user == null) + return null; + var tosend = new UserSend(); + tosend.pfp = user.pfp; + tosend.username = user.username; + tosend._id = user._id; + tosend.creationDate = user.creationDate; + tosend.email = user.email; + return tosend; + } } } |