aboutsummaryrefslogtreecommitdiff
path: root/Backend/Api/Api/Services/UserService.cs
diff options
context:
space:
mode:
Diffstat (limited to 'Backend/Api/Api/Services/UserService.cs')
-rw-r--r--Backend/Api/Api/Services/UserService.cs66
1 files changed, 65 insertions, 1 deletions
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;
+ }
}
}