diff options
author | Ognjen Cirkovic <ciraboxkg@gmail.com> | 2022-11-16 21:58:32 +0100 |
---|---|---|
committer | Ognjen Cirkovic <ciraboxkg@gmail.com> | 2022-11-16 21:58:32 +0100 |
commit | e8e7d6f9b3ca17cb79cf7f2936f248f2ce59fba9 (patch) | |
tree | e69619d2dde859e4304ad972183b665e0d46c7cd /Backend/Api | |
parent | c8eb6ba248aff872a4f4d2aa7ccde50aacad73a6 (diff) |
Dodati modeli za message, napravljen message service. Napravljen ChatHub(SignalR) servis, omoguceno cuvanje sesija clienata pri konekciji.
Diffstat (limited to 'Backend/Api')
-rw-r--r-- | Backend/Api/Api/Controllers/UserController.cs | 9 | ||||
-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/Message.cs | 27 | ||||
-rw-r--r-- | Backend/Api/Api/Services/ChatHub.cs | 59 | ||||
-rw-r--r-- | Backend/Api/Api/Services/MessageService.cs | 61 | ||||
-rw-r--r-- | Backend/Api/Api/appsettings.json | 3 |
7 files changed, 160 insertions, 1 deletions
diff --git a/Backend/Api/Api/Controllers/UserController.cs b/Backend/Api/Api/Controllers/UserController.cs index 1ef8234..94e4b41 100644 --- a/Backend/Api/Api/Controllers/UserController.cs +++ b/Backend/Api/Api/Controllers/UserController.cs @@ -55,5 +55,14 @@ namespace Api.Controllers return Ok(rez); return BadRequest(); } + [HttpGet("{id}/profile")] + [Authorize(Roles = "User")] + public async Task<ActionResult<UserSend>> GetUserById(string id) + { + var rez = await _userService.getUserById(id); + if (rez != null) + return Ok(rez); + return BadRequest(); + } } } diff --git a/Backend/Api/Api/Database/DatabaseConnection.cs b/Backend/Api/Api/Database/DatabaseConnection.cs index f26b88e..eaf047a 100644 --- a/Backend/Api/Api/Database/DatabaseConnection.cs +++ b/Backend/Api/Api/Database/DatabaseConnection.cs @@ -10,5 +10,6 @@ namespace Api.Database public string PostCollectionName { get; set; } = String.Empty; public string FileCollectionName { get; set; } = String.Empty; public string LocationCollectionName { get; set; } = String.Empty; + public string MessageCollectionname { get; set; }= String.Empty; } } diff --git a/Backend/Api/Api/Interfaces/IDatabaseConnection.cs b/Backend/Api/Api/Interfaces/IDatabaseConnection.cs index 17b5262..a8fec9a 100644 --- a/Backend/Api/Api/Interfaces/IDatabaseConnection.cs +++ b/Backend/Api/Api/Interfaces/IDatabaseConnection.cs @@ -8,5 +8,6 @@ string PostCollectionName { get; set; } string FileCollectionName { get; set; } string LocationCollectionName { get; set; } + string MessageCollectionname { get; set; } } } diff --git a/Backend/Api/Api/Models/Message.cs b/Backend/Api/Api/Models/Message.cs new file mode 100644 index 0000000..d3b6785 --- /dev/null +++ b/Backend/Api/Api/Models/Message.cs @@ -0,0 +1,27 @@ +using MongoDB.Bson.Serialization.Attributes; +using MongoDB.Bson; + +namespace Api.Models +{ + public class Message + { + [BsonId] + [BsonRepresentation(BsonType.ObjectId)] + public String _id { get; set; } + public String senderId { get; set; } + public String receiverId { get; set; } + public String messagge { get; set; } + public DateTime timestamp { get; set; } + } + public class MessageSend + { + public String senderId { get; set; } + public String messagge { get; set; } + public DateTime timestamp { get; set; } + } + public class MessageReceive + { + public String receiverId { get; set; } + public String messagge { get; set; } + } +} diff --git a/Backend/Api/Api/Services/ChatHub.cs b/Backend/Api/Api/Services/ChatHub.cs new file mode 100644 index 0000000..c196065 --- /dev/null +++ b/Backend/Api/Api/Services/ChatHub.cs @@ -0,0 +1,59 @@ +using Microsoft.AspNetCore.SignalR; + +namespace Api.Services +{ + public class ChatHub:Hub + { + static public readonly Dictionary<string, string> Users = new Dictionary<string, string>(); + private readonly JwtService _jwtService; + public ChatHub(JwtService jwtService) + { + _jwtService = jwtService; + } + public override async Task OnConnectedAsync() + { + string token = Context.GetHttpContext().Request.Query["access_token"]; + if (token == null) + return; + string id = _jwtService.TokenToId(token); + if (id == null) + return; + Users.Add(Context.ConnectionId, id); + //await SendDirect(id, "poruka"); + //await Send(Context.ConnectionId); + await base.OnConnectedAsync(); + + } + public override async Task OnDisconnectedAsync(Exception? exception) + { + Users.Remove(Context.ConnectionId); + } + public static List<string> getAllConnectionsOfUser(string id) + { + List<string> keys = new List<string>(); + foreach (var user in Users) + { + if (user.Value == id) + keys.Add(user.Key); + } + return keys; + } + } +} +//private readonly IHubContext<ChatHub> _ichat; +//await _ichat.Clients.Client(connection).SendAsync("Message",); + +//Message +//SenderId +//meesage +//date + +//Message +//ReceiveId +//meesage + + +//Oba korisnika online poruka se salje preko kontrolera, a stize preko socketa +//Online salje offline poruka se salje preko kontrolera,cuva se u bazi +//Korisnik koji ima poruke a bio je offline salje zahtev kontroleru za preuzimanje poruka +///chatHub
\ No newline at end of file diff --git a/Backend/Api/Api/Services/MessageService.cs b/Backend/Api/Api/Services/MessageService.cs new file mode 100644 index 0000000..0772b48 --- /dev/null +++ b/Backend/Api/Api/Services/MessageService.cs @@ -0,0 +1,61 @@ +using System.Security.Claims; +using Api.Interfaces; +using Api.Models; +using MongoDB.Driver; + +namespace Api.Services +{ + public class MessageService + { + private readonly IHttpContextAccessor _httpContext; + private readonly IMongoCollection<Message> _messages; + private readonly IJwtService _jwtService; + private IConfiguration _configuration; + private readonly IFileService _fileService; + public MessageService(IDatabaseConnection settings, IMongoClient mongoClient, IJwtService jwtService, IHttpContextAccessor httpContextAccessor, IConfiguration configuration) + { + var database = mongoClient.GetDatabase(settings.DatabaseName); + _messages= database.GetCollection<Message>(settings.UserCollectionName); + _jwtService = jwtService; + } + public async Task<Message> addMessage(MessageReceive msg) + { + var senderId = _httpContext.HttpContext.User.FindFirstValue("id").ToString(); + if (senderId == null) + return null; + var tempMsg = new Message(); + tempMsg._id = ""; + tempMsg.receiverId = msg.receiverId; + tempMsg.senderId = senderId; + tempMsg.messagge = msg.messagge; + tempMsg.timestamp= DateTime.Now.ToUniversalTime(); + + await _messages.InsertOneAsync(tempMsg); + + return tempMsg; + } + public MessageSend messageToMessageSend(Message msg) + { + var tempMsg=new MessageSend(); + tempMsg.senderId = msg.senderId; + tempMsg.messagge=msg.messagge; + tempMsg.timestamp = msg.timestamp; + return tempMsg; + } + public async Task<List<MessageSend>> getReceiverMessage() + { + var receiverId = _httpContext.HttpContext.User.FindFirstValue("id").ToString(); + if (receiverId == null) + return null; + var messages = await _messages.Find(msg => msg.receiverId == receiverId).ToListAsync(); + var tempMessages = new List<MessageSend>(); + foreach (var message in messages) + { + tempMessages.Add(messageToMessageSend(message)); + _messages.DeleteOne(msg=>msg._id==message._id); + } + return tempMessages; + } + + } +} diff --git a/Backend/Api/Api/appsettings.json b/Backend/Api/Api/appsettings.json index a3ef323..7dbaa6a 100644 --- a/Backend/Api/Api/appsettings.json +++ b/Backend/Api/Api/appsettings.json @@ -21,7 +21,8 @@ "UserCollectionName": "users", "PostCollectionName": "posts", "FileCollectionName": "files", - "LocationCollectionname": "locations" + "LocationCollectionname": "locations", + "MessageCollectionname": "messages" }, "EmailCfg": { |