#Query("UPDATE RealEstateDatabase SET type = :entryType WHERE id = :id AND type NOT LIKE :entryType")
suspend fun updateRealEstate(entryType: String, id: String)
This code works perfectly to update a row in the table based on a condition I want to do the same thing with several rows each linked to a condition
here is the entity/table in question
#Entity
#Parcelize
data class RealEstateDatabase(
#PrimaryKey
var id: String,
var type: String? = null,
var price: Int? = null,
var area: Int? = null,
var numberRoom: String? = null,
var description: String? = null,
var numberAndStreet: String? = null,
var numberApartment: String? = null,
var city: String? = null,
var region: String? = null,
var postalCode: String? = null,
var country: String? = null,
var status: String? = null,
var dateOfEntry: String? = null,
var dateOfSale: String? = null,
var realEstateAgent: String? = null,
var lat: Double ?=null,
var lng: Double ?=null,
var hospitalsNear : Boolean = false,
var schoolsNear : Boolean = false,
var shopsNear : Boolean = false,
var parksNear : Boolean = false,
#ColumnInfo(name = "listPhotoWithText")
var listPhotoWithText : List<PhotoWithTextFirebase> ?=null,
var count_photo : Int? = listPhotoWithText?.size,
)
I also put the method of my repository , this method has parameters of the same type as my table to modify
override suspend fun updateRealEstate(
id: String,
entryType: String,
entryPrice: String,
entryArea: String,
entryNumberRoom: String,
entryDescription: String,
entryNumberAndStreet: String,
entryNumberApartement: String,
entryCity: String,
entryRegion: String,
entryPostalCode: String,
entryCountry: String,
entryStatus: String,
textDateOfEntry: String,
textDateOfSale: String,
realEstateAgent: String?,
lat: Double?,
lng: Double?,
checkedStateHopital: MutableState<Boolean>,
checkedStateSchool: MutableState<Boolean>,
checkedStateShops: MutableState<Boolean>,
checkedStateParks: MutableState<Boolean>,
listPhotoWithText: List<PhotoWithTextFirebase>?,
itemRealEstate: RealEstateDatabase
): Response<Boolean> {
return try {
Response.Loading
val rEcollection = firebaseFirestore.collection("real_estates")
if(entryType != itemRealEstate.type ){
rEcollection.document(id).update("type",entryType)
}
realEstateDao.updateRealEstate(entryType,id)
Response.Success(true)
}catch (e: Exception) {
Response.Failure(e)
}
}
I repeat that I want to update the rows of the table on the condition that the variable given by the method of my repo is different from the row in question
#Query("UPDATE RealEstateDatabase SET " +
"type = (CASE WHEN type NOT LIKE :entryType THEN (:entryType) ELSE type END) ," +
"price = (CASE WHEN price NOT LIKE :entryPrice THEN (:entryPrice) ELSE price END) WHERE id =:id")
suspend fun updateRealEstate(
entryType: String,
id: String,
entryPrice: Int
)
this solution works but forces me to update the value anyway
What you can do is add a condition in the WHERE clause which will prevent the execution of the UPDATE statement if both columns do not need to be updated:
#Query("UPDATE RealEstateDatabase SET " +
"type = CASE WHEN type NOT LIKE :entryType THEN (:entryType) ELSE type END," +
"price = CASE WHEN price NOT LIKE :entryPrice THEN (:entryPrice) ELSE price END " +
"WHERE id = :id AND (type NOT LIKE :entryType OR price NOT LIKE :entryPrice)")
Related
I have a dataclass:
data class MoviesInSeries(
val originalMovieName: String,
// If there's no value of parameter, assign it as null
val movieInSeries1Name: String? = null,
val movieInSeries1Date: String? = null,
val movieInSeries1Rating: String? = null,
val movieInSeries1Pic: Int? = null,
val movieInSeries2Name: String? = null,
val movieInSeries2Date: String? = null,
val movieInSeries2Rating: String? = null,
val movieInSeries2Pic: Int? = null,
)
I've created two objects of it.
fun getRestOfSeriesMovies(): ArrayList<MoviesInSeries> {
val movieList = ArrayList<MoviesInSeries>()
val s_gi_joe = MoviesInSeries("G.I. Joe: Retaliation", "G.I. Joe: The Rise of Cobra",
"2009","Pg-13", R.drawable.gijtsofmp)
movieList.add(s_gi_joe)
val s_gi_joe2 = MoviesInSeries("G.I. Joe: Retaliation2", "G.I. Joe: The Rise of Cobra",
"2009","Pg-13", R.drawable.gijtsofmp)
movieList.add(s_gi_joe)
return movieList
}
Now I want to create an array list of the originalMovieName member. I'll later use that to check if a certain string is in it. How would I do that?
there's a lot of weird things happening in your model class :
1-
checkIFMovieNameExist(getRestOfSeriesMovies())
fun checkIFMovieNameExist(list: List<MoviesInSeries>,value:String): MoviesInSeries? {
return list.find { it.originalMovieName == value }
}
2-from time of creation:
data class MoviesInSeries(
val originalMovieName: String,
// If there's no value of parameter, assign it as null
val movieInSeries1Name: String? = null,
val movieInSeries1Date: String? = null,
val movieInSeries1Rating: String? = null,
val movieInSeries1Pic: Int? = null,
val movieInSeries2Name: String? = null,
val movieInSeries2Date: String? = null,
val movieInSeries2Rating: String? = null,
val movieInSeries2Pic: Int? = null,
){
init {
names.add(originalMovieName)
}
companion object {
private val names: MutableList<String> = mutableListOf()
fun getMoviesNames():List<String>{
return names
}
}
}
then you can check if a name exist in names list in any place in the code
MoviesInSeries("sd1")
MoviesInSeries("sd2")
MoviesInSeries("sd3")
MoviesInSeries("sd4")
println(MoviesInSeries. getMoviesNames().contains("sd1"))
Following is my code
var items : MutableList<Any> = arrayListOf()
items.add(TeacherDetails(it?.photo,it?.firstName,it?.lastName,it?.level))
items.add(TeacherBio(it?.bio))
items.add(TitleAccreditations(getString(R.string.acreditations)))
items.add(SessionsTitle(it?.firstName + getString(R.string.apostrophe) + getString(
R.string.sessions)))
items.addAll(listOf(it?.classes ?: arrayListOf()))
items.add(IntroVideo(it?.introVideo))
items.addAll(it?.teachingAccreditations?.split("\n")?.map { Accreditation(
it
) }?: emptyList())
Issue is at following line it is adding entire list as object instead of individual item.
items.addAll(listOf(it?.classes ?: arrayListOf()))
Following is my model
data class Teacher(
#field:SerializedName("firstName")
val firstName: String? = null,
#field:SerializedName("lastName")
val lastName: String? = null,
#field:SerializedName("teacherId")
val teacherId: String? = null,
#field:SerializedName("introVideo")
val introVideo: String? = null,
#field:SerializedName("level")
val level: String? = null,
#field:SerializedName("teachingAccreditations")
val teachingAccreditations: String? = null,
#field:SerializedName("classes")
val classes: List<ClassesItem?>? = null,
#field:SerializedName("photo")
val photo: String? = null,
#field:SerializedName("bio")
val bio: String? = null
)
I guess because your type of list is Any, it will consider adding list instance also as an object, so instead it as listOf() just add directly
items.addAll(it?.classes?.filterNotNull()?: arrayListOf())
How do i declare an id with primary key auto increment?
package com.ncf.globofly.models
data class Destination(
var id: Int = 0, #PRIMAY KEY Auto Increment
var Sequence: String? = null,
var Description: String? = null,
var Status: String? = null
)
It can be done using annotation first add the dependencies for database and annotation processor after that
data class Destination(
#PrimaryKey(autoGenerate = true)
var id: Int = 0,
var Sequence: String? = null,
var Description: String? = null,
var Status: String? = null
)
I have these types:
data class Match(
#PrimaryKey var uid: Long? = null,
#ColumnInfo var homeTeamId: Long? = null,
#ColumnInfo var awayTeamId: Long? = null,
#Ignore var homeTeam: Team? = null,
#Ignore var awayTeam: Team? = null
)
data class Team(
#PrimaryKey var uid: Long? = null,
#ColumnInfo var captainId: Long? = null,
#Ignore var captain: Player? = null
)
data class Player(
#PrimaryKey var uid: Long? = null,
#ColumnInfo var name: String? = null,
var number: Int? = null
)
and I'm getting match as a LiveData:
// inside fragment
matchDao.getLive(matchId).observe(this, Observer {})
I want to set homeTeam on match using the homeTeamId property. How can I use transformations to set the values for homeTeam, awayTeam and captain?
This is how far I have gotten:
fun getMatchLive(id: Long): LiveData<Match> {
return Transformations.switchMap(matchDao.getLive(id)) { inputMatch ->
val output1: LiveData<Match>? = inputMatch.homeTeamId?.run {
Transformations.switchMap(teamDao.getLive(this)) { team ->
team.captainId?.run {
Transformations.map(playerDao.getLive(this)) { player ->
team.apply {
captain = player
inputMatch.homeTeam = team
}
inputMatch
}
}
}
}
output1?.run {
Transformations.switchMap(this) { inputMatch1 ->
inputMatch1.awayTeamId?.run {
Transformations.switchMap(teamDao.getLive(this)) { team ->
team.captainId?.run {
Transformations.map(playerDao.getLive(this)) { player ->
team.apply {
captain = player
inputMatch1.awayTeam = team
}
inputMatch1
}
}
}
}
}
}
}
}
but I'm almost certain that's neither the right way to do it nor is it maintainable. For example, if I want to add a new property to the match variable, I have to transform the result of output1.run.
I have a scenario. I have created a data class in Kotlin like this:
data class AgentDetails(
val mobileNo: String,
val Name: String,
val Email: String,
val Password: String,
val Occupation: String,
val pincode: String,
val usertype: String,
val profilepic: String,
val AccountStatus: String
)
I want to send different type of objects of this data class to a web service:
1st object example:
val agentDetails = AgentDetails(mobileNo = mobileNumberText.text.toString(),
Name = userNameText.text.toString(),
Email = emailIdText.text.toString(),
Password = HashUtils.sha1(passwordText.text.toString()),
Occupation = item,
pincode = pinCodeText.text.toString(),
usertype = "Agent",
profilepic = "null", AccountStatus = "pending")
In 2nd object I only want to send mobile number. I dont wanna include any other field. Something like this:
val agentDetails = AgentDetails(mobileNo = mobileNumberText.text.toString())
And in 3rd object I only wanna send email id. Instead of creating multiple data classes. Can I use the same data class for multiple implementations?
Personally, I'd define three objects because they represent three different concepts (or projections of a concept). But if you make your properties nullable and provide a default value of null, you can get away with creating them as you want...
data class AgentDetails(
val mobileNo: String? = null,
val name: String? = null,
val email: String? = null,
val password: String? = null,
val occupation: String? = null,
val pincode: String? = null,
val usertype: String? = null,
val profilepic: String? = null,
val accountStatus: String? = null
)
Note: I've changed some of your property names to camelCase, as is the proper convention. And these all work fine:
AgentDetails(mobileNo = mobileNumberText.text.toString())
AgentDetails(email = "foo#example.com")
AgentDetails(name = "Foo", password = "Bar")
All of the other fields not provided will be null, and the types will be nullable, so you'll have to guard against that. Otherwise, I'd define three data classes for this.
Another solution would be to consider a sealed class structure:
sealed class AgentDetails
data class AgentByName(val name: String) : AgentDetails()
data class AgentByEmail(val email: String): AgentDetails()
// etc..
And then use it in a when expression:
fun doSomethingWithAgents(agentDetails: AgentDetails) {
when (agentDetails) {
is AgentByName -> // Do something
is AgentByEmail -> // Do Something
}
}
The easiest way is to make the fields nullable and provide default values:
data class AgentDetails(
val mobileNo: String? = null,
val Name: String? = null,
val Email: String? = null,
val Password: String? = null,
val Occupation: String? = null,
val pincode: String? = null,
val usertype: String? = null,
val profilepic: String? = null,
val AccountStatus: String? = null
)