Skip to content

Commit

Permalink
Fix get photos bug with many2many (#176)
Browse files Browse the repository at this point in the history
* Fix get photos bug with many2many

Also made a new PhotoTagReferenceDto to help handle this better.

* add responseentity to get request

* remove unused imports and smal cleanup

* Update src/main/kotlin/no/fg/hilflingbackend/controller/MotiveController.kt

Co-authored-by: Sindre J. I. Sivertsen <sindrejohan1@gmail.com>

* Update src/main/kotlin/no/fg/hilflingbackend/controller/PhotoController.kt

Co-authored-by: Sindre J. I. Sivertsen <sindrejohan1@gmail.com>

* cleanup

* remove unused method

* remove unused method

* lint

Co-authored-by: Caroline Sandsbråten <carolinesandsbraten@Carolines-MBP.dhcp.wlan.samfundet.no>
Co-authored-by: Sindre J. I. Sivertsen <sindrejohan1@gmail.com>
  • Loading branch information
3 people authored Feb 5, 2022
1 parent 5c76de1 commit 42e9ccb
Show file tree
Hide file tree
Showing 9 changed files with 137 additions and 88 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-kotlin</artifactId>
</dependency>
<dependency>
<dependency>
<groupId>me.liuwj.ktorm</groupId>
<artifactId>ktorm-jackson</artifactId>
<version>3.1.0</version>
Expand Down
30 changes: 12 additions & 18 deletions src/main/kotlin/no/fg/hilflingbackend/controller/PhotoController.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package no.fg.hilflingbackend.controller

import me.liuwj.ktorm.database.Database
import no.fg.hilflingbackend.dto.PhotoDto
import no.fg.hilflingbackend.exceptions.GlobalExceptionHandler
import no.fg.hilflingbackend.model.AnalogPhoto
Expand All @@ -24,18 +25,9 @@ import java.util.UUID
@RestController
@RequestMapping("/photos")
class PhotoController(
val photoService: PhotoService
val photoService: PhotoService,
val database: Database
) : GlobalExceptionHandler() {
// TODO: Remove not used anyMOre
@PostMapping("/profile", consumes = ["multipart/form-data"])
private fun uploadPhotoFile(
@RequestPart("file") file: MultipartFile,
// @RequestPart("type") type: String,
): String {
// TODO: Refactor to use photoDto as in photoService
// return photoService.store(file, SecurityLevelType.valueOf("PROFILE"))
return "TODO"
}

// The main photo-upload endpoint used most of the time
@PostMapping("/upload")
Expand All @@ -53,9 +45,9 @@ class PhotoController(
): ResponseEntity<List<String>> =
ResponseEntity(
photoService.createNewMotiveAndSaveDigitalPhotos(
motiveTitle = motiveTitle,
placeName = placeName,
eventOwnerName = eventOwnerName,
motiveString = motiveTitle,
placeString = placeName,
eventOwnerString = eventOwnerName,
securityLevelId = securityLevelId,
albumId = albumId,
photoGangBangerId = photoGangBangerId,
Expand Down Expand Up @@ -149,10 +141,12 @@ class PhotoController(
)

@GetMapping
fun getAll(): ResponseEntity<List<PhotoDto>> = ResponseOk(
photoService
.getAll(),
)
fun getAll(): ResponseEntity<List<PhotoDto>> {
return ResponseOk(
photoService
.getAll()
)
}

@GetMapping("/carousel")
fun getCarouselPhotos(): ResponseEntity<List<PhotoDto>> = ResponseOk(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import no.fg.hilflingbackend.dto.PhotoGangBangerDto
import no.fg.hilflingbackend.repository.PhotoGangBangerRepository
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PatchMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
Expand Down Expand Up @@ -43,9 +44,16 @@ class PhotoGangBangerController {
return repository.findAllInActivePangs()
}

@PostMapping
@PostMapping("/create")
fun create(
@RequestBody photoGangBangerDto: PhotoGangBangerDto
@RequestBody dto: PhotoGangBangerDto
): Int =
repository.create(photoGangBangerDto)
repository.create(dto)

@PatchMapping()
fun patch(
@RequestBody dto: PhotoGangBangerDto
): Int {
return repository.patch(dto)
}
}
27 changes: 27 additions & 0 deletions src/main/kotlin/no/fg/hilflingbackend/dto/PhotoTagReferenceDto.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package no.fg.hilflingbackend.dto

import no.fg.hilflingbackend.model.Photo
import no.fg.hilflingbackend.model.PhotoTag
import no.fg.hilflingbackend.model.PhotoTagReference
import java.util.UUID

data class PhotoTagReferenceDto(
val photoTagReferenceId: PhotoTagReferenceId = PhotoTagReferenceId(),
val photoTag: PhotoTag,
val photo: Photo
)

data class PhotoTagReferenceId(
override val id: UUID = UUID.randomUUID()
) : UuidId {
override fun toString(): String = id.toString()
}

fun PhotoTagReferenceDto.toEntity(): PhotoTagReference {
val dto = this
return PhotoTagReference {
id = dto.photoTagReferenceId.id
photo = dto.photo
photoTag = dto.photoTag
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,17 @@ import me.liuwj.ktorm.database.Database
import me.liuwj.ktorm.entity.Entity
import me.liuwj.ktorm.entity.sequenceOf
import me.liuwj.ktorm.schema.uuid
import java.util.UUID

interface PhotoTagReference : BaseModel<PhotoTagReference> {
companion object : Entity.Factory<PhotoTagReference>()

var photoTag: UUID
var photo: UUID
var photoTag: PhotoTag
var photo: Photo
}

object PhotoTagReferences : BaseTable<PhotoTagReference>("photo_tag_in_photo") {
val photoTagId = uuid("photo_tag_id").bindTo { it.photoTag }
val photoId = uuid("photo_id").bindTo { it.photo }
val photoTagId = uuid("photo_tag_id").references(PhotoTags) { it.photoTag }
val photoId = uuid("photo_id").references(Photos) { it.photo }
}

val Database.photo_tag_references get() = this.sequenceOf(PhotoTagReferences)
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import me.liuwj.ktorm.entity.add
import me.liuwj.ktorm.entity.filter
import me.liuwj.ktorm.entity.find
import me.liuwj.ktorm.entity.toList
import me.liuwj.ktorm.entity.update
import no.fg.hilflingbackend.dto.PhotoGangBangerDto
import no.fg.hilflingbackend.dto.toEntity
import no.fg.hilflingbackend.model.photo_gang_bangers
Expand Down Expand Up @@ -55,9 +56,15 @@ open class PhotoGangBangerRepository {
}

fun create(
photoGangBangerDto: PhotoGangBangerDto
dto: PhotoGangBangerDto
): Int =
database.photo_gang_bangers.add(
photoGangBangerDto.toEntity()
dto.toEntity()
)

fun patch(
dto: PhotoGangBangerDto
): Int = database.photo_gang_bangers.update(
dto.toEntity()
)
}
114 changes: 62 additions & 52 deletions src/main/kotlin/no/fg/hilflingbackend/repository/PhotoRepository.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package no.fg.hilflingbackend.repository

import me.liuwj.ktorm.database.Database
import me.liuwj.ktorm.dsl.batchInsert
import me.liuwj.ktorm.dsl.crossJoin
import me.liuwj.ktorm.dsl.eq
import me.liuwj.ktorm.dsl.from
Expand All @@ -16,75 +17,72 @@ import me.liuwj.ktorm.entity.update
import no.fg.hilflingbackend.dto.PhotoDto
import no.fg.hilflingbackend.dto.PhotoTagDto
import no.fg.hilflingbackend.dto.PhotoTagId
/* ktlint-disable no-wildcard-imports */
import no.fg.hilflingbackend.model.*
import no.fg.hilflingbackend.model.Albums
import no.fg.hilflingbackend.model.AnalogPhoto
import no.fg.hilflingbackend.model.Motives
import no.fg.hilflingbackend.model.Photo
import no.fg.hilflingbackend.model.PhotoTagReferences
import no.fg.hilflingbackend.model.PhotoTags
import no.fg.hilflingbackend.model.SecurityLevel
import no.fg.hilflingbackend.model.SecurityLevels
import no.fg.hilflingbackend.model.analog_photos
import no.fg.hilflingbackend.model.photos
import no.fg.hilflingbackend.model.toDto
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Repository
import java.util.UUID

/* TODO Remove this?
fun Database.getPhotoAndPhotoTags(filter: () -> ColumnDeclaring<Boolean>){
return this
.from(Photos)
.crossJoin(PhotoTags)
.select(
Photos.id,
PhotoTags.name
)
}
*/
@Repository
open class PhotoRepository {
@Autowired
open lateinit var database: Database

open class PhotoRepository(
val database: Database
) {
val logger = LoggerFactory.getLogger(this::class.java)

private fun findCorrespondingPhotoTagDtos(photo: Photo): List<PhotoTagDto> {
return database.from(PhotoTags)
.crossJoin(PhotoTagReferences)
.select(
PhotoTags.name,
PhotoTags.id,
PhotoTagReferences.photoId,
PhotoTagReferences.photoTagId
)
.where { PhotoTagReferences.photoId eq photo.id }
.map { row ->
PhotoTagDto(
photoTagId = PhotoTagId(row[PhotoTags.id]!!),
name = row[PhotoTags.name]!!
)
}
}

fun findById(id: UUID): PhotoDto? {
return database.photos.find { it.id eq id }
?.let { photo ->
val tags = database
.from(PhotoTags)
.crossJoin(PhotoTagReferences)
.select(
PhotoTags.id,
PhotoTags.name,
PhotoTagReferences.photoId,
PhotoTagReferences.photoTagId
)
.where { PhotoTagReferences.photoId eq photo.id }
.map { row ->
PhotoTagDto(
// TODO: Try to avoid !! null safety override
photoTagId = PhotoTagId(row[PhotoTags.id]!!),
name = row[PhotoTags.name]!!
)
}
return photo.toDto(tags)
return photo.toDto(
findCorrespondingPhotoTagDtos(photo)
)
}
}

fun findByMotiveId(id: UUID): List<PhotoDto>? {
return database
.photos
.filter {
it.motiveId eq id
}
.toList()
.map { it.toDto() }
return database.photos.filter {
it.motiveId eq id
}.toList()
.map { it.toDto(findCorrespondingPhotoTagDtos(it)) }
}

fun findAnalogPhotoById(id: UUID): AnalogPhoto? {
return database.analog_photos.find { it.id eq id }
}

fun findAll(): List<PhotoDto> {
return database
.photos
.toList()
.map { it.toDto() }
return database.photos.toList()
.map {
it.toDto(
findCorrespondingPhotoTagDtos(it)
)
}
}

fun findAllAnalogPhotos(): List<PhotoDto> {
Expand All @@ -96,7 +94,7 @@ open class PhotoRepository {
album.isAnalog eq true
}
.toList()
.map { it.toDto() }
.map { it.toDto(findCorrespondingPhotoTagDtos(it)) }
}

fun findAllDigitalPhotos(): List<PhotoDto> {
Expand All @@ -107,15 +105,15 @@ open class PhotoRepository {
val album = motive.albumId.referenceTable as Albums
album.isAnalog eq false
}.toList()
.map { it.toDto() }
.map { it.toDto(findCorrespondingPhotoTagDtos(it)) }
}

fun findCarouselPhotos(): List<PhotoDto> {
return database
.photos
.filter { it.isGoodPicture eq true }
.take(6).toList()
.map { it.toDto() }
.map { it.toDto(findCorrespondingPhotoTagDtos(it)) }
}

fun findBySecurityLevel(securityLevel: SecurityLevel): List<PhotoDto> {
Expand All @@ -125,7 +123,7 @@ open class PhotoRepository {
val securityLevelFromDatabase = it.securityLevelId.referenceTable as SecurityLevels
securityLevelFromDatabase.id eq securityLevel.id
}.toList()
.map { it.toDto() }
.map { it.toDto(findCorrespondingPhotoTagDtos(it)) }
}

fun createPhoto(
Expand All @@ -146,6 +144,18 @@ open class PhotoRepository {
}
}
*/
logger.info("Storing photo tags to database")
val photoTagDtoList = photoDto.photoTags
database.batchInsert(PhotoTagReferences) {
photoTagDtoList.map { photoTagDto ->
item {
set(it.id, UUID.randomUUID())
set(it.photoTagId, photoTagDto.photoTagId.id)
set(it.photoId, photoDto.photoId.id)
}
}
}

return numOfSavedPhotos
}

Expand Down
12 changes: 6 additions & 6 deletions src/main/kotlin/no/fg/hilflingbackend/service/PhotoService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -280,13 +280,13 @@ class PhotoService(
}

override fun createNewMotiveAndSaveDigitalPhotos(
motiveTitle: String,
placeName: String,
motiveString: String,
placeString: String,
securityLevelId: UUID,
photoGangBangerId: UUID,
albumId: UUID,
categoryName: String,
eventOwnerName: String,
eventOwnerString: String,
photoFileList: List<MultipartFile>,
isGoodPhotoList: List<Boolean>,
tagList: List<List<String>>
Expand All @@ -299,7 +299,7 @@ class PhotoService(
if (!isValidRequest) throw java.lang.IllegalArgumentException("photoFileList, isGoodPhotoList and tagList are of unequal length or not given")
logger.info("createNewMotiveAndSaveDigitalPhotos() $tagList")
val eventOwnerDto = eventOwnerRepository
.findByEventOwnerName(EventOwnerName.valueOf(eventOwnerName))
.findByEventOwnerName(EventOwnerName.valueOf(eventOwnerString))
?: throw EntityNotFoundException("Did not find eventOwner")

val albumDto = albumRepository
Expand All @@ -322,10 +322,10 @@ class PhotoService(
// Fetch object from database, if not exist create object
// and save to database
// TODO: Wait with saving place to database to later?
val placeDto = fetchOrCreatePlaceDto(placeName)
val placeDto = fetchOrCreatePlaceDto(placeString)

val motiveDto = fetchOrCreateMotive(
motiveTitle = motiveTitle,
motiveTitle = motiveString,
eventOwnerDto = eventOwnerDto,
categoryDto = categoryDto,
albumDto = albumDto
Expand Down
Loading

0 comments on commit 42e9ccb

Please sign in to comment.