From e8e7d6f9b3ca17cb79cf7f2936f248f2ce59fba9 Mon Sep 17 00:00:00 2001 From: Ognjen Cirkovic Date: Wed, 16 Nov 2022 21:58:32 +0100 Subject: Dodati modeli za message, napravljen message service. Napravljen ChatHub(SignalR) servis, omoguceno cuvanje sesija clienata pri konekciji. --- Backend/Api/Api/Controllers/UserController.cs | 9 ++++ Backend/Api/Api/Database/DatabaseConnection.cs | 1 + Backend/Api/Api/Interfaces/IDatabaseConnection.cs | 1 + Backend/Api/Api/Models/Message.cs | 27 ++++++++++ Backend/Api/Api/Services/ChatHub.cs | 59 ++++++++++++++++++++++ Backend/Api/Api/Services/MessageService.cs | 61 +++++++++++++++++++++++ Backend/Api/Api/appsettings.json | 3 +- 7 files changed, 160 insertions(+), 1 deletion(-) create mode 100644 Backend/Api/Api/Models/Message.cs create mode 100644 Backend/Api/Api/Services/ChatHub.cs create mode 100644 Backend/Api/Api/Services/MessageService.cs (limited to 'Backend/Api') 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> 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 Users = new Dictionary(); + 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 getAllConnectionsOfUser(string id) + { + List keys = new List(); + foreach (var user in Users) + { + if (user.Value == id) + keys.Add(user.Key); + } + return keys; + } + } +} +//private readonly IHubContext _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 _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(settings.UserCollectionName); + _jwtService = jwtService; + } + public async Task 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> 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(); + 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": { -- cgit v1.2.3 From b221f3dd432c8666d522817a63064c6f9193a637 Mon Sep 17 00:00:00 2001 From: Ognjen Cirkovic Date: Wed, 16 Nov 2022 22:16:40 +0100 Subject: Napravljen controller za dodavanje poruka i vracanje primljenih poruka u slicaju da smo offline. Fixed typo --- Backend/Api/Api/Controllers/MessageController.cs | 44 ++++++++++++++++++++++++ Backend/Api/Api/Interfaces/IMessageService.cs | 11 ++++++ Backend/Api/Api/Program.cs | 1 + Backend/Api/Api/Services/MessageService.cs | 18 ++++++---- 4 files changed, 68 insertions(+), 6 deletions(-) create mode 100644 Backend/Api/Api/Controllers/MessageController.cs create mode 100644 Backend/Api/Api/Interfaces/IMessageService.cs (limited to 'Backend/Api') 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> 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>> getMyMessages() + { + var msgTemp = await _messageService.getReceiverMessage(); + if (msgTemp == null) + return BadRequest(); + return Ok(msgTemp); + } + } +} 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 addMessage(MessageReceive msg); + Task> getReceiverMessage(); + MessageSend messageToMessageSend(Message msg); + } +} \ No newline at end of file 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(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); +builder.Services.AddScoped(); builder.Services.AddHttpContextAccessor(); diff --git a/Backend/Api/Api/Services/MessageService.cs b/Backend/Api/Api/Services/MessageService.cs index 0772b48..0e48285 100644 --- a/Backend/Api/Api/Services/MessageService.cs +++ b/Backend/Api/Api/Services/MessageService.cs @@ -5,7 +5,7 @@ using MongoDB.Driver; namespace Api.Services { - public class MessageService + public class MessageService : IMessageService { private readonly IHttpContextAccessor _httpContext; private readonly IMongoCollection _messages; @@ -15,11 +15,15 @@ namespace Api.Services public MessageService(IDatabaseConnection settings, IMongoClient mongoClient, IJwtService jwtService, IHttpContextAccessor httpContextAccessor, IConfiguration configuration) { var database = mongoClient.GetDatabase(settings.DatabaseName); - _messages= database.GetCollection(settings.UserCollectionName); + _messages = database.GetCollection(settings.MessageCollectionname); _jwtService = jwtService; + _httpContext = httpContextAccessor; + _configuration = configuration; } public async Task 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; @@ -28,17 +32,19 @@ namespace Api.Services tempMsg.receiverId = msg.receiverId; tempMsg.senderId = senderId; tempMsg.messagge = msg.messagge; - tempMsg.timestamp= DateTime.Now.ToUniversalTime(); + 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(); + var tempMsg = new MessageSend(); tempMsg.senderId = msg.senderId; - tempMsg.messagge=msg.messagge; + tempMsg.messagge = msg.messagge; tempMsg.timestamp = msg.timestamp; return tempMsg; } @@ -52,7 +58,7 @@ namespace Api.Services foreach (var message in messages) { tempMessages.Add(messageToMessageSend(message)); - _messages.DeleteOne(msg=>msg._id==message._id); + _messages.DeleteOne(msg => msg._id == message._id); } return tempMessages; } -- cgit v1.2.3 From d1838298af4a0c91509ebafa194de2b3805f6210 Mon Sep 17 00:00:00 2001 From: Ognjen Cirkovic Date: Wed, 16 Nov 2022 22:22:37 +0100 Subject: Aktiviran websocket servis. --- Backend/Api/Api/Program.cs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) (limited to 'Backend/Api') diff --git a/Backend/Api/Api/Program.cs b/Backend/Api/Api/Program.cs index 280a9dd..8d22f2b 100644 --- a/Backend/Api/Api/Program.cs +++ b/Backend/Api/Api/Program.cs @@ -65,6 +65,10 @@ builder.Services.AddSwaggerGen(options => { options.OperationFilter(); }); + +builder.Services.AddSignalR(); + + builder.Services.AddCors(options => { options.AddPolicy("CorsPolicy", @@ -85,11 +89,20 @@ if (app.Environment.IsDevelopment()) app.UseSwaggerUI(); } +app.UseWebSockets(new WebSocketOptions +{ + KeepAliveInterval = TimeSpan.FromSeconds(120) +}); //Add Authentication +app.UseRouting(); app.UseAuthentication(); app.UseAuthorization(); -app.MapControllers(); +app.UseEndpoints(endpoints => +{ + endpoints.MapControllers(); + endpoints.MapHub("/chatHub"); +}); app.Run(); -- cgit v1.2.3 From d49c17b695f131f9faf2e419e163df80dde37a88 Mon Sep 17 00:00:00 2001 From: Ognjen Cirkovic Date: Thu, 17 Nov 2022 21:28:43 +0100 Subject: Omoguceno ukoliko je korisnik ulogovan da mu se poruka salje preko socketa, ukoliko nije ulogovan poruke se cuvaju u bazi. --- Backend/Api/Api/Controllers/MessageController.cs | 2 +- Backend/Api/Api/Services/ChatHub.cs | 12 ++++++++++++ Backend/Api/Api/Services/MessageService.cs | 21 +++++++++++++++++---- 3 files changed, 30 insertions(+), 5 deletions(-) (limited to 'Backend/Api') diff --git a/Backend/Api/Api/Controllers/MessageController.cs b/Backend/Api/Api/Controllers/MessageController.cs index 63351d0..750fc5f 100644 --- a/Backend/Api/Api/Controllers/MessageController.cs +++ b/Backend/Api/Api/Controllers/MessageController.cs @@ -24,7 +24,7 @@ namespace Api.Controllers } [HttpPost("add")] [Authorize(Roles = "User")] - public async Task> addMessage([FromBody] MessageReceive msg) + public async Task> addMessage([FromBody] MessageReceive msg) { var msgTemp = await _messageService.addMessage(msg); if(msgTemp == null) diff --git a/Backend/Api/Api/Services/ChatHub.cs b/Backend/Api/Api/Services/ChatHub.cs index c196065..4092d8f 100644 --- a/Backend/Api/Api/Services/ChatHub.cs +++ b/Backend/Api/Api/Services/ChatHub.cs @@ -19,6 +19,7 @@ namespace Api.Services if (id == null) return; Users.Add(Context.ConnectionId, id); + //await SendDirect(id, "poruka"); //await Send(Context.ConnectionId); await base.OnConnectedAsync(); @@ -38,6 +39,17 @@ namespace Api.Services } return keys; } + public static bool CheckUser(string id) + { + var users = Users.Values; + foreach (var user in users) + { + if (user == id) + return true; + } + return false; + } + } } //private readonly IHubContext _ichat; diff --git a/Backend/Api/Api/Services/MessageService.cs b/Backend/Api/Api/Services/MessageService.cs index 0e48285..9cc818b 100644 --- a/Backend/Api/Api/Services/MessageService.cs +++ b/Backend/Api/Api/Services/MessageService.cs @@ -1,6 +1,7 @@ using System.Security.Claims; using Api.Interfaces; using Api.Models; +using Microsoft.AspNetCore.SignalR; using MongoDB.Driver; namespace Api.Services @@ -11,14 +12,15 @@ namespace Api.Services private readonly IMongoCollection _messages; private readonly IJwtService _jwtService; private IConfiguration _configuration; - private readonly IFileService _fileService; - public MessageService(IDatabaseConnection settings, IMongoClient mongoClient, IJwtService jwtService, IHttpContextAccessor httpContextAccessor, IConfiguration configuration) + private readonly IHubContext _chatHub; + public MessageService(IDatabaseConnection settings, IMongoClient mongoClient, IJwtService jwtService, IHttpContextAccessor httpContextAccessor, IConfiguration configuration,IHubContext chatHub) { var database = mongoClient.GetDatabase(settings.DatabaseName); _messages = database.GetCollection(settings.MessageCollectionname); _jwtService = jwtService; _httpContext = httpContextAccessor; _configuration = configuration; + _chatHub = chatHub; } public async Task addMessage(MessageReceive msg) { @@ -33,10 +35,21 @@ namespace Api.Services tempMsg.senderId = senderId; tempMsg.messagge = msg.messagge; tempMsg.timestamp = DateTime.Now.ToUniversalTime(); + if (ChatHub.CheckUser(msg.receiverId)) + { + //user online + var temp =messageToMessageSend(tempMsg); + foreach (var connection in ChatHub.getAllConnectionsOfUser(msg.receiverId)) + await _chatHub.Clients.Client(connection).SendAsync("Message",temp); + } + else + { + //user offline + await _messages.InsertOneAsync(tempMsg); - //TODO if user online send thru socket - await _messages.InsertOneAsync(tempMsg); + } + return tempMsg; } -- cgit v1.2.3 From 91a2519d714fadecfcc14a8fb88b511f23fc21ed Mon Sep 17 00:00:00 2001 From: Ognjen Cirkovic Date: Fri, 18 Nov 2022 21:56:36 +0100 Subject: Dodato dugme za izbor lokacije na stranici za dodavanje objave. Omoguceno dodavanje lokacije sa koordinata i osnovnim informacijama o lokaciji. --- Backend/Api/Api/Services/LocationService.cs | 10 +++--- .../brzodolokacije/Activities/ActivityAddPost.kt | 37 ++++++++++++++++------ .../brzodolokacije/Activities/MapsActivity.kt | 2 +- .../app/src/main/res/layout/activity_add_post.xml | 16 ++++++++-- 4 files changed, 49 insertions(+), 16 deletions(-) (limited to 'Backend/Api') diff --git a/Backend/Api/Api/Services/LocationService.cs b/Backend/Api/Api/Services/LocationService.cs index c91a1b5..afb3b5b 100644 --- a/Backend/Api/Api/Services/LocationService.cs +++ b/Backend/Api/Api/Services/LocationService.cs @@ -31,10 +31,12 @@ namespace Api.Services } public async Task add(Location loc) { - IEnumerable
adresses = await _geocoder.GeocodeAsync(loc.name+" "+loc.address+" "+loc.city+" "+loc.country); - loc.country = loc.name; - loc.latitude = adresses.First().Coordinates.Latitude; - loc.longitude=adresses.First().Coordinates.Longitude; + + //On Client side + //IEnumerable
adresses = await _geocoder.GeocodeAsync(loc.name+" "+loc.address+" "+loc.city+" "+loc.country); + //loc.country = loc.name; + //loc.latitude = adresses.First().Coordinates.Latitude; + //loc.longitude=adresses.First().Coordinates.Longitude; await _locations.InsertOneAsync(loc); return loc; diff --git a/Client/BrzoDoLokacije/app/src/main/java/com/example/brzodolokacije/Activities/ActivityAddPost.kt b/Client/BrzoDoLokacije/app/src/main/java/com/example/brzodolokacije/Activities/ActivityAddPost.kt index b9e4fd7..d5712cc 100644 --- a/Client/BrzoDoLokacije/app/src/main/java/com/example/brzodolokacije/Activities/ActivityAddPost.kt +++ b/Client/BrzoDoLokacije/app/src/main/java/com/example/brzodolokacije/Activities/ActivityAddPost.kt @@ -17,16 +17,15 @@ import com.example.brzodolokacije.Models.Location import com.example.brzodolokacije.Models.LocationType import com.example.brzodolokacije.Models.PostPreview import com.example.brzodolokacije.R +import com.example.brzodolokacije.Services.GeocoderHelper import com.example.brzodolokacije.Services.RetrofitHelper import com.example.brzodolokacije.Services.SharedPreferencesHelper -import okhttp3.MediaType import okhttp3.MediaType.Companion.toMediaTypeOrNull import okhttp3.MultipartBody import okhttp3.RequestBody import retrofit2.Call import retrofit2.Response import java.io.File -import java.io.IOException class ActivityAddPost : AppCompatActivity() { @@ -42,6 +41,11 @@ class ActivityAddPost : AppCompatActivity() { private lateinit var locationString:String private lateinit var descriptionString:String private lateinit var post:Button + private lateinit var addLocation:Button + val incorectCoord:Double=1000.0 + val LOCATIONREQCODE=123 + var longitude:Double=incorectCoord + var latitude:Double=incorectCoord //private var paths :ArrayList?=null private var place=0; override fun onCreate(savedInstanceState: Bundle?) { @@ -61,6 +65,7 @@ class ActivityAddPost : AppCompatActivity() { location=findViewById(R.id.etActivityAddPostLocation) as EditText description=findViewById(R.id.etActivityAddPostDescription) as EditText post=findViewById(R.id.btnActivityAddPostPost) as Button + addLocation=findViewById(R.id.btnActivityAddPostAddLocation) as Button switcher?.setFactory{ @@ -68,6 +73,10 @@ class ActivityAddPost : AppCompatActivity() { imgView.scaleType = ImageView.ScaleType.CENTER_CROP imgView.setPadding(8, 8, 8, 8) imgView} + addLocation.setOnClickListener { + val myIntent = Intent(this, MapsActivity::class.java) + startActivityForResult(myIntent,LOCATIONREQCODE) + } //dodavanje iz galerije uploadFromGallery.setOnClickListener{ @@ -116,7 +125,7 @@ class ActivityAddPost : AppCompatActivity() { descriptionString=description.text.toString().trim() //prazan unos? if(locationString.isEmpty()) { - location.hint="Unesite lokaciju" + location.hint="Unesite naziv lokaciju" location.setHintTextColor(Color.RED) } if(descriptionString.isEmpty()) { @@ -124,7 +133,7 @@ class ActivityAddPost : AppCompatActivity() { description.setHintTextColor(Color.RED) } - if(!locationString.isEmpty() && !descriptionString.isEmpty()){ + if(!locationString.isEmpty() && !descriptionString.isEmpty() && longitude!=incorectCoord && latitude!=incorectCoord){ sendPost() } } @@ -162,14 +171,26 @@ class ActivityAddPost : AppCompatActivity() { uploadFromGallery.isVisible=false } } + if(requestCode==LOCATIONREQCODE && resultCode== RESULT_OK){ + var bundle=data!!.extras + longitude=bundle!!.getDouble("longitude",incorectCoord) + latitude=bundle!!.getDouble("latitude",incorectCoord) + } } private fun sendPost(){ uploadLocation() } fun uploadLocation() { + //TO DO SEARCH EXISTING LOCATION FROM DB + //IF NOT EXISTS ADD NEW LOCATION val api =RetrofitHelper.getInstance() - var loc:Location=Location("",locationString,"","","",0.0,0.0,LocationType.GRAD) + var geocoder=GeocoderHelper.getInstance() + var loc1=geocoder!!.getFromLocation(latitude,longitude,1) + var countryName=loc1[0].countryName + var address="todo not possible in query" + var city=loc1[0].adminArea//not possible + var loc:Location=Location("",locationString,city,countryName,address,latitude,longitude,LocationType.GRAD) var jwtString= SharedPreferencesHelper.getValue("jwt",this) var data=api.addLocation("Bearer "+jwtString,loc) @@ -177,12 +198,11 @@ class ActivityAddPost : AppCompatActivity() { data.enqueue(object : retrofit2.Callback { override fun onResponse(call: Call, response: Response) { if(response.isSuccessful()){ + + uploadPost(response.body()!!._id) Toast.makeText( applicationContext, "USPEH", Toast.LENGTH_LONG ).show(); - uploadPost(response.body()!!._id) - Log.d("MAIN","RADI") - Log.d("MAIN","RADI") }else { @@ -190,7 +210,6 @@ class ActivityAddPost : AppCompatActivity() { Log.d("Main",response.errorBody()!!.string()) Log.d("Main",response.message()) } - Log.d("Main","sadadsa") Log.d("Main",response.errorBody()!!.string()) Log.d("Main",response.message()) } diff --git a/Client/BrzoDoLokacije/app/src/main/java/com/example/brzodolokacije/Activities/MapsActivity.kt b/Client/BrzoDoLokacije/app/src/main/java/com/example/brzodolokacije/Activities/MapsActivity.kt index e8b9ceb..b9e3b08 100644 --- a/Client/BrzoDoLokacije/app/src/main/java/com/example/brzodolokacije/Activities/MapsActivity.kt +++ b/Client/BrzoDoLokacije/app/src/main/java/com/example/brzodolokacije/Activities/MapsActivity.kt @@ -78,7 +78,7 @@ class MapsActivity : AppCompatActivity() { } confirmButton.setOnClickListener{ if(locLatitude!=null && locLatitude!=null) - returnValue() + returnValue() } searchBar.setOnKeyListener(View.OnKeyListener { v1, keyCode, event -> // If the event is a key-down event on the "enter" button if (event.action === KeyEvent.ACTION_DOWN && diff --git a/Client/BrzoDoLokacije/app/src/main/res/layout/activity_add_post.xml b/Client/BrzoDoLokacije/app/src/main/res/layout/activity_add_post.xml index a7a6e06..6c9b40f 100644 --- a/Client/BrzoDoLokacije/app/src/main/res/layout/activity_add_post.xml +++ b/Client/BrzoDoLokacije/app/src/main/res/layout/activity_add_post.xml @@ -73,7 +73,7 @@ +