aboutsummaryrefslogtreecommitdiff
path: root/Backend/Api
diff options
context:
space:
mode:
Diffstat (limited to 'Backend/Api')
-rw-r--r--Backend/Api/Api/Controllers/MessageController.cs44
-rw-r--r--Backend/Api/Api/Controllers/UserController.cs9
-rw-r--r--Backend/Api/Api/Database/DatabaseConnection.cs1
-rw-r--r--Backend/Api/Api/Interfaces/IDatabaseConnection.cs1
-rw-r--r--Backend/Api/Api/Interfaces/IMessageService.cs11
-rw-r--r--Backend/Api/Api/Models/Message.cs27
-rw-r--r--Backend/Api/Api/Program.cs1
-rw-r--r--Backend/Api/Api/Services/ChatHub.cs59
-rw-r--r--Backend/Api/Api/Services/MessageService.cs67
-rw-r--r--Backend/Api/Api/appsettings.json3
10 files changed, 222 insertions, 1 deletions
diff --git a/Backend/Api/Api/Controllers/MessageController.cs b/Backend/Api/Api/Controllers/MessageController.cs
new file mode 100644
index 0000000..63351d0
--- /dev/null
+++ b/Backend/Api/Api/Controllers/MessageController.cs
@@ -0,0 +1,44 @@
+using System.Data;
+using Api.Interfaces;
+using Api.Models;
+using Api.Services;
+using Microsoft.AspNetCore.Authorization;
+using Microsoft.AspNetCore.Http;
+using Microsoft.AspNetCore.Mvc;
+
+namespace Api.Controllers
+{
+ [Route("api/[controller]")]
+ [ApiController]
+ public class MessageController : ControllerBase
+ {
+
+ private readonly IJwtService _jwtService;
+ private readonly IMessageService _messageService;
+ public MessageController( IJwtService jwtService,IMessageService messageService)
+ {
+
+ _jwtService = jwtService;
+ _messageService = messageService;
+
+ }
+ [HttpPost("add")]
+ [Authorize(Roles = "User")]
+ public async Task<ActionResult<MessageSend>> addMessage([FromBody] MessageReceive msg)
+ {
+ var msgTemp = await _messageService.addMessage(msg);
+ if(msgTemp == null)
+ return BadRequest();
+ return Ok(msgTemp);
+ }
+ [HttpGet("myMessages")]
+ [Authorize(Roles = "User")]
+ public async Task<ActionResult<List<MessageSend>>> getMyMessages()
+ {
+ var msgTemp = await _messageService.getReceiverMessage();
+ if (msgTemp == null)
+ return BadRequest();
+ return Ok(msgTemp);
+ }
+ }
+}
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/Interfaces/IMessageService.cs b/Backend/Api/Api/Interfaces/IMessageService.cs
new file mode 100644
index 0000000..712b344
--- /dev/null
+++ b/Backend/Api/Api/Interfaces/IMessageService.cs
@@ -0,0 +1,11 @@
+using Api.Models;
+
+namespace Api.Interfaces
+{
+ public interface IMessageService
+ {
+ Task<Message> addMessage(MessageReceive msg);
+ Task<List<MessageSend>> getReceiverMessage();
+ MessageSend messageToMessageSend(Message msg);
+ }
+} \ No newline at end of file
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/Program.cs b/Backend/Api/Api/Program.cs
index 7d6b03e..280a9dd 100644
--- a/Backend/Api/Api/Program.cs
+++ b/Backend/Api/Api/Program.cs
@@ -27,6 +27,7 @@ builder.Services.AddScoped<IJwtService, JwtService>();
builder.Services.AddScoped<IPostService,PostService>();
builder.Services.AddScoped<IFileService,FileService>();
builder.Services.AddScoped<ILocationService,LocationService>();
+builder.Services.AddScoped<IMessageService, MessageService>();
builder.Services.AddHttpContextAccessor();
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..0e48285
--- /dev/null
+++ b/Backend/Api/Api/Services/MessageService.cs
@@ -0,0 +1,67 @@
+using System.Security.Claims;
+using Api.Interfaces;
+using Api.Models;
+using MongoDB.Driver;
+
+namespace Api.Services
+{
+ public class MessageService : IMessageService
+ {
+ 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.MessageCollectionname);
+ _jwtService = jwtService;
+ _httpContext = httpContextAccessor;
+ _configuration = configuration;
+ }
+ public async Task<Message> addMessage(MessageReceive msg)
+ {
+ if (_httpContext.HttpContext.User.FindFirstValue("id") == null)
+ return null;
+ 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();
+
+
+ //TODO if user online send thru socket
+ 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": {