diff options
author | TAMARA JERINIC <tamara.jerinic@gmail.com> | 2022-11-14 23:29:19 +0100 |
---|---|---|
committer | TAMARA JERINIC <tamara.jerinic@gmail.com> | 2022-11-14 23:29:19 +0100 |
commit | f7bd4d189a2fa635bb4b156381a7b105bf75ab6d (patch) | |
tree | 824e3ac8f4afc91d9a4013f6605ac364dac775a6 /Backend/Api | |
parent | d98ff2f7d8a15793b5cc92bd430160c53eb3f8ca (diff) | |
parent | 81fae5e55c7a2b5a1ddc8055d32bc310e22e014d (diff) |
Merge branch 'develop' of http://gitlab.pmf.kg.ac.rs/BrzoDoLokacije2022/odyssey/brzodolokacije into develop
# Conflicts:
# Client/BrzoDoLokacije/app/src/main/java/com/example/brzodolokacije/Fragments/FragmentProfile.kt
# Client/BrzoDoLokacije/app/src/main/java/com/example/brzodolokacije/Fragments/FragmentUserPosts.kt
# Client/BrzoDoLokacije/app/src/main/res/layout/fragment_user_posts.xml
Diffstat (limited to 'Backend/Api')
-rw-r--r-- | Backend/Api/Api/Controllers/PostController.cs | 7 | ||||
-rw-r--r-- | Backend/Api/Api/Controllers/UserController.cs | 59 | ||||
-rw-r--r-- | Backend/Api/Api/Interfaces/IPostService.cs | 3 | ||||
-rw-r--r-- | Backend/Api/Api/Interfaces/IUserService.cs | 4 | ||||
-rw-r--r-- | Backend/Api/Api/Models/User.cs | 11 | ||||
-rw-r--r-- | Backend/Api/Api/Services/PostService.cs | 19 | ||||
-rw-r--r-- | Backend/Api/Api/Services/UserService.cs | 66 |
7 files changed, 160 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..1ef8234 --- /dev/null +++ b/Backend/Api/Api/Controllers/UserController.cs @@ -0,0 +1,59 @@ +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; + private readonly IPostService _postService; + public UserController(IUserService userService, IJwtService jwtService, IPostService postService) + { + _userService = userService; + _jwtService = jwtService; + _postService = postService; + } + [HttpPost("profile/pfp")] + [Authorize(Roles = "User")] + public async Task<ActionResult<User>> setPfp([FromForm]IFormFile image) + { + var id = await _userService.UserIdFromJwt(); + 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(); + } + [HttpGet("posts")] + [Authorize(Roles = "User")] + public async Task<ActionResult<List<PostSend>>> SelfPosts() + { + var id = await _userService.UserIdFromJwt(); + var rez = await _postService.GetUsersPosts(id); + 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..fc2ae03 100644 --- a/Backend/Api/Api/Interfaces/IPostService.cs +++ b/Backend/Api/Api/Interfaces/IPostService.cs @@ -10,12 +10,13 @@ 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); Task CascadeDeleteComments(string cmntid, Post p); Task<PostSendPage> SearchPosts(string locid, int page = 0, int sorttype = 1, int filterdate = 1); int DateEnumToDays(int filterdate); + Task<List<PostSend>> GetUsersPosts(string id); } }
\ No newline at end of file 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..2e323a8 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,14 @@ 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; } + public int postcount { get; set; } + } } diff --git a/Backend/Api/Api/Services/PostService.cs b/Backend/Api/Api/Services/PostService.cs index a0b2941..2d62f49 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) @@ -336,5 +336,18 @@ namespace Api.Services default: return 365 * 10; } } + public async Task<List<PostSend>> GetUsersPosts(string id) + { + var userposts = await _posts.Find(x => x.ownerId == id).ToListAsync(); + if (userposts == null) + return null; + var tosend = new List<PostSend>(); + foreach (var post in userposts) + { + var x = await postToPostSend(post); + tosend.Add(x); + } + return tosend; + } } } diff --git a/Backend/Api/Api/Services/UserService.cs b/Backend/Api/Api/Services/UserService.cs index 5fd61f6..33e6d11 100644 --- a/Backend/Api/Api/Services/UserService.cs +++ b/Backend/Api/Api/Services/UserService.cs @@ -12,15 +12,19 @@ namespace Api.Services { private readonly IHttpContextAccessor _httpContext; private readonly IMongoCollection<User> _users; + private readonly IMongoCollection<Post> _posts; 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); + _posts = database.GetCollection<Post>(settings.PostCollectionName); _jwtService = jwtService; this._httpContext = httpContextAccessor; this._configuration = configuration; + _fileService = fileService; } public async Task<int> createUser(User user) @@ -311,5 +315,65 @@ 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 =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.name = user.name; + tosend.pfp = user.pfp; + tosend.username = user.username; + tosend._id= user._id; + tosend.creationDate = user.creationDate; + tosend.email=""; + var userposts = await _posts.Find(x => x.ownerId == user._id).ToListAsync(); + tosend.postcount = userposts.Count(); + 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.name = user.name; + tosend.pfp = user.pfp; + tosend.username = user.username; + tosend._id = user._id; + tosend.creationDate = user.creationDate; + tosend.email = user.email; + var userposts = await _posts.Find(x => x.ownerId == user._id).ToListAsync(); + tosend.postcount = userposts.Count(); + return tosend; + } } } |