Skip to content

Commit

Permalink
Support approving antechamber songs
Browse files Browse the repository at this point in the history
  • Loading branch information
igrek51 committed Feb 19, 2020
1 parent 603eaba commit ae649d5
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ class AdminSongsLayoutContoller : InflatedLayout(
},
ContextMenuBuilder.Action(R.string.admin_antechamber_approve_action) {
ConfirmDialogBuilder().confirmAction(R.string.admin_antechamber_confirm_approve) {
// Todo
approveAntechamberSong(song)
}
},
ContextMenuBuilder.Action(R.string.admin_antechamber_delete_action) {
Expand All @@ -116,6 +116,18 @@ class AdminSongsLayoutContoller : InflatedLayout(
))
}

private fun approveAntechamberSong(song: Song) {
uiInfoService.showInfoIndefinite(R.string.admin_sending)
antechamberService.approveAntechamberSong(song)
.observeOn(AndroidSchedulers.mainThread())
.subscribe({
uiInfoService.showInfo(R.string.admin_success)
}, { error ->
val message = uiResourceService.resString(R.string.admin_communication_breakdown, error.message)
uiInfoService.showInfoIndefinite(message)
})
}

private fun updateAntechamberSong(song: Song) {
uiInfoService.showInfoIndefinite(R.string.admin_sending)
antechamberService.updateAntechamberSong(song)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,13 @@ class AntechamberService {
lateinit var customSongService: CustomSongService

companion object {
private const val apiUrl = "https://antechamber.chords.igrek.dev/api/v4"
private const val allSongsUrl = "$apiUrl/songs"
private val specificSongUrl = { id: Long -> "$apiUrl/songs/$id" }
private const val antechamberApiBase = "https://antechamber.chords.igrek.dev/api/v4"
private const val chordsApiBase = "https://chords.igrek.dev/api/v5"

private const val allSongsUrl = "$antechamberApiBase/songs"
private val specificSongUrl = { id: Long -> "$antechamberApiBase/songs/$id" }
private const val approveSongUrl = "$chordsApiBase/songs"

private const val authTokenHeader = "X-Auth-Token"
}

Expand Down Expand Up @@ -103,6 +107,23 @@ class AntechamberService {
return httpRequest(request) { true }
}

fun approveAntechamberSong(song: Song): Observable<Boolean> {
logger.info("Approving antechamber song: $song")
val dto = ChordsSongDto.fromModel(song)
dto.id = null
val mapper = jacksonObjectMapper()
val json = mapper.writeValueAsString(dto)
val request: Request = Request.Builder()
.url(approveSongUrl)
.post(RequestBody.create(jsonType, json))
.addHeader(authTokenHeader, adminService.get().userAuthToken)
.build()
return httpRequest(request) { response: Response ->
logger.debug("Approve response", response.body()?.string())
true
}
}

private fun <T> httpRequest(request: Request, successor: (Response) -> T): Observable<T> {
val receiver = BehaviorSubject.create<T>()

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package igrek.songbook.admin.antechamber

import igrek.songbook.persistence.general.model.Song
import igrek.songbook.settings.chordsnotation.ChordsNotation

data class ChordsSongDto(
var id: Long? = null,
var title: String? = null,
var categories: List<String> = emptyList(),
var content: String? = null,
var version_number: Long? = 1,
var create_time: Long? = 0,
var update_time: Long? = 0,
var language: String? = null,
var chords_notation: Long? = null,
var author: String? = null,
var preferred_key: String? = null,
var metre: String? = null,
var comment: String? = null,
var is_locked: Boolean? = false,
var lock_password: String? = null,
var scroll_speed: Double? = null,
var initial_delay: Double? = null,
var state: Long? = null,
var rank: Double? = null,
var tags: String? = null
) {

companion object {
fun fromModel(song: Song): ChordsSongDto = ChordsSongDto(
id = song.id,
title = song.title,
categories = emptyList(),
content = song.content,
version_number = song.versionNumber,
create_time = song.createTime,
update_time = song.updateTime,
language = song.language,
chords_notation = (song.chordsNotation ?: ChordsNotation.default).id,
author = song.author,
preferred_key = song.preferredKey,
metre = song.metre,
comment = song.comment,
is_locked = song.locked,
lock_password = song.lockPassword,
scroll_speed = song.scrollSpeed,
initial_delay = song.initialDelay,
state = song.status.id,
rank = song.rank,
tags = song.tags
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,17 @@ class SongsUpdater {
songsRepository.get().reloadSongsDb()
uiInfoService.get().showInfo(R.string.ui_db_is_uptodate)
} catch (t: Throwable) {
logger.error("Reloading songs db failed: ${t.message}")
uiInfoService.get().showInfo(R.string.db_update_failed_incompatible)
songsRepository.get().resetGeneralData()
songsRepository.get().reloadSongsDb()
}
}
} catch (e: Throwable) {
onErrorReceived(e.message)
logger.error("Failed saving new db: ${e.message}")
Handler(Looper.getMainLooper()).post {
uiInfoService.get().showInfoIndefinite(R.string.connection_error)
}
}
}

Expand Down
Binary file modified app/src/main/res/raw/songs.sqlite
Binary file not shown.

0 comments on commit ae649d5

Please sign in to comment.