From d945204fba909a2dfeb25cae2d320ab6e62eaf51 Mon Sep 17 00:00:00 2001 From: Ognjen Cirkovic Date: Mon, 28 Nov 2022 11:23:53 +0100 Subject: Promenjen zahtev za pretragu lokacije na backendu. Omogucena pretraga lokacija na clientu, prikazuje se naziv lokacije. --- Backend/Api/Api/Controllers/LocationController.cs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'Backend/Api') diff --git a/Backend/Api/Api/Controllers/LocationController.cs b/Backend/Api/Api/Controllers/LocationController.cs index c9ef9ba..ba9c9a8 100644 --- a/Backend/Api/Api/Controllers/LocationController.cs +++ b/Backend/Api/Api/Controllers/LocationController.cs @@ -51,10 +51,15 @@ namespace Api.Controllers return BadRequest(); } - [HttpPost("search")] + [HttpGet("search")] [Authorize(Roles = "User")] - public async Task>> searchLocation(int searchtype ,string? query,Coords? coords) + public async Task>> searchLocation(int searchtype ,string? query,double? latitude,double? longitude) { + Coords coords = new Coords(); + if (latitude!=null && longitude!=null) { + coords.latitude = (double)latitude; + coords.longitude = (double)longitude; + } List ret = new List(); switch (searchtype) { -- cgit v1.2.3 From 9c3cebe6f89586d2abd5f854cf5baa079d839b39 Mon Sep 17 00:00:00 2001 From: Ognjen Cirkovic Date: Mon, 28 Nov 2022 11:54:31 +0100 Subject: Napravljen dialog za dodavanje imena lokacije. Dodata logika dodavanje lokacije. Uplodovanje lokacije. Dodata provera na backu ukoliko nema tagova. Omoguceno dodavanje objave pomocu novog dodavanja lokacije. --- Backend/Api/Api/Services/PostService.cs | 7 +- .../brzodolokacije/Activities/ActivityAddPost.kt | 74 ++------------- .../brzodolokacije/Activities/MapsActivity.kt | 102 ++++++++++++++++++--- 3 files changed, 101 insertions(+), 82 deletions(-) (limited to 'Backend/Api') diff --git a/Backend/Api/Api/Services/PostService.cs b/Backend/Api/Api/Services/PostService.cs index b75656e..e2e4e40 100644 --- a/Backend/Api/Api/Services/PostService.cs +++ b/Backend/Api/Api/Services/PostService.cs @@ -39,8 +39,11 @@ namespace Api.Services p.comments = new List(); p.images = new List(); p.createdAt = DateTime.Now.ToUniversalTime(); - var tags = post.tags.Split("|").ToList(); - p.tags = tags; + if (post.tags != null) + { + var tags = post.tags.Split("|").ToList(); + p.tags = tags; + } var folderPath = Path.Combine(Directory.GetCurrentDirectory(), "Files", p.ownerId); if (!Directory.Exists(folderPath)) { 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 f29db17..c2abf37 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 @@ -53,33 +53,27 @@ class ActivityAddPost : AppCompatActivity() { private lateinit var tagButtonAdd:Button private lateinit var tagList: MutableList private var tagidcounter:Int = 0 - val incorectCoord:Double=1000.0 val LOCATIONREQCODE=123 - var longitude:Double=incorectCoord - var latitude:Double=incorectCoord + var locationId:String?=null var progressDialog:ProgressDialog?=null private lateinit var addDescription:Button - //private var paths :ArrayList?=null + private var place=0; override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_add_post) -// Toast.makeText( -// applicationContext, "Add new ", Toast.LENGTH_LONG -// ).show(); + uploadedImages= ArrayList() tagList = mutableListOf() tagButtons= mutableListOf() tagidcounter = 0 - //paths= ArrayList() uploadFromGallery=findViewById(R.id.btnActivityAddPostUploadFromGalleryVisible) as Button showNextImage=findViewById(R.id.nextImage) as Button showPreviousImage=findViewById(R.id.previousImage) as Button switcher=findViewById(R.id.isActivityAddPostSwitcher) as ImageSwitcher - //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 @@ -205,11 +199,11 @@ class ActivityAddPost : AppCompatActivity() { description.hint="Unesite opis" description.setHintTextColor(Color.RED) } - if(longitude!=incorectCoord && latitude!=incorectCoord){ + if(locationId==null || locationId!!.trim()==""){ Toast.makeText(this,"Unesite lokaciju klikom na dugme",Toast.LENGTH_LONG) } - if(!locationString.isEmpty() && !descriptionString.isEmpty() && longitude!=incorectCoord && latitude!=incorectCoord && uploadedImages!!.size>0){ + if(!descriptionString.isEmpty() && uploadedImages!!.size>0){ sendPost() } } @@ -249,9 +243,7 @@ class ActivityAddPost : AppCompatActivity() { } if(requestCode==LOCATIONREQCODE && resultCode== RESULT_OK){ var bundle=data!!.extras - longitude=bundle!!.getDouble("longitude",incorectCoord) - latitude=bundle!!.getDouble("latitude",incorectCoord) - var locName=bundle!!.getString("name") + locationId=bundle!!.getString("locationId") } } private fun sendPost(){ @@ -259,58 +251,8 @@ class ActivityAddPost : AppCompatActivity() { } fun uploadLocation() { - //TO DO SEARCH EXISTING LOCATION FROM DB - //IF NOT EXISTS ADD NEW LOCATION - progressDialog!!.show() - val api =RetrofitHelper.getInstance() - var geocoder=GeocoderHelper.getInstance() - var loc1=geocoder!!.getFromLocation(latitude,longitude,1) - if(loc1==null ||loc1.size<=0) - { - progressDialog!!.dismiss() - Toast.makeText(this,"Lokacija ne postoji",Toast.LENGTH_LONG); - return - } - var countryName=loc1[0].countryName - var address="todo not possible in query" - var city="its null" - if(loc1[0].adminArea!=null) - 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) - - 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(); - - }else { - progressDialog!!.dismiss() - - if (response.errorBody() != null) { - Log.d("Main",response.errorBody()!!.string()) - Log.d("Main",response.message()) - } - Log.d("Main",response.errorBody()!!.string()) - Log.d("Main",response.message()) - } - - - } - - override fun onFailure(call: Call, t: Throwable) { - Toast.makeText( - applicationContext, t.toString(), Toast.LENGTH_LONG - ).show(); - Log.d("Main",t.toString()) - progressDialog!!.dismiss() - } - }) + if(locationId!=null && locationId!!.trim()!="") + uploadPost(locationId!!) } fun uploadPost(loc:String){ val api =RetrofitHelper.getInstance() 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 46cc5fe..faaa3b4 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 @@ -1,7 +1,9 @@ package com.example.brzodolokacije.Activities import android.Manifest +import android.app.AlertDialog import android.content.Context +import android.content.DialogInterface import android.content.pm.PackageManager import android.graphics.Color import android.location.Location @@ -22,6 +24,7 @@ import androidx.appcompat.app.AppCompatActivity import androidx.core.app.ActivityCompat import androidx.core.content.ContextCompat import androidx.core.widget.addTextChangedListener +import com.example.brzodolokacije.Models.LocationType import com.example.brzodolokacije.Models.PostPreview import com.example.brzodolokacije.R import com.example.brzodolokacije.Services.GeocoderHelper @@ -88,8 +91,12 @@ class MapsActivity : AppCompatActivity() { getLocation() } confirmButton.setOnClickListener{ - if(locLatitude!=null && locLatitude!=null) + if(selectedLocation!=null) returnValue() + else{ + addLocation() + + } } 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 && @@ -115,6 +122,73 @@ class MapsActivity : AppCompatActivity() { + } + fun addLocation(){ + var editText=EditText(this) + var dialog=AlertDialog.Builder(this).setTitle("Naziv").setMessage("Unesite naziv") + .setView(editText) + dialog.setPositiveButton("Dodaj") { dialog, which -> + uploadLocation(editText.text.toString()) + } + dialog.setNegativeButton("Prekini") { dialog, which -> + + } + dialog.show() + + } + fun uploadLocation(locationName:String){ + val api =RetrofitHelper.getInstance() + var geocoder=GeocoderHelper.getInstance() + var loc1=geocoder!!.getFromLocation(locLatitude!!,locLongitude!!,1) + if(loc1==null ||loc1.size<=0) + { + return + } + var countryName=loc1[0].countryName + var address="todo not possible in query" + var city="its null" + if(loc1[0].adminArea!=null) + city=loc1[0].adminArea//not possible + var loc: com.example.brzodolokacije.Models.Location = + com.example.brzodolokacije.Models.Location( + "", + locationName, + city, + countryName, + address, + locLatitude!!, + locLongitude!!, + LocationType.GRAD + ) + var jwtString= SharedPreferencesHelper.getValue("jwt",this) + var data=api.addLocation("Bearer "+jwtString,loc) + + data.enqueue(object : retrofit2.Callback { + override fun onResponse(call: Call, response: Response) { + if(response.isSuccessful()){ + selectedLocation=response.body() + returnValue() + + }else { + + if (response.errorBody() != null) { + Log.d("Main",response.errorBody()!!.string()) + Log.d("Main",response.message()) + } + Log.d("Main",response.errorBody()!!.string()) + Log.d("Main",response.message()) + } + + + } + + override fun onFailure(call: Call, t: Throwable) { + Toast.makeText( + applicationContext, t.toString(), Toast.LENGTH_LONG + ).show(); + Log.d("Main",t.toString()) + } + }) } var arraySpinner :MutableList?=null var spinnerAdapter: ArrayAdapter?=null @@ -128,10 +202,22 @@ class MapsActivity : AppCompatActivity() { searchBar.setAdapter(spinnerAdapter) searchBar.setOnItemClickListener(OnItemClickListener { parent, view, position, id -> val selected = parent.getItemAtPosition(position) as String - Log.d("Main",selected) + selectedLocation=responseLocations!!.find { location -> location.name==selected } + Log.d("main",selectedLocation.toString()) + confirmButton.visibility=View.VISIBLE }) + } + fun returnValue(){ + val intent = intent + val bundle = Bundle() + if(selectedLocation==null) + return + bundle.putString("locationId",selectedLocation!!._id) + intent.putExtras(bundle) + setResult(RESULT_OK, intent) + finish() } fun onTextEnter(){ var api=RetrofitHelper.getInstance() @@ -164,18 +250,6 @@ class MapsActivity : AppCompatActivity() { } - fun returnValue(){ - val intent = intent - val bundle = Bundle() - bundle.putDouble("longitude", locLongitude!!) - bundle.putDouble("latitude", locLatitude!!) - if(searchBar.text!=null && !searchBar.text.toString().equals("")) - bundle.putString("name", searchBar.text.toString()) - intent.putExtras(bundle) - setResult(RESULT_OK, intent) - finish() - } - override fun onResume() { super.onResume() map!!.onResume() -- cgit v1.2.3