GitBucket
4.23.0
Toggle navigation
Sign in
Files
Branches
1
Releases
Issues
Pull requests
Labels
Priorities
Milestones
Wiki
Forks
yhornisse
/
bremer
Browse code
fix tag api
master
1 parent
31922ba
commit
3128abe26be63c682c285c2337f1e126efb439ab
yhornisse
authored
on 25 Aug 2023
Patch
Showing
2 changed files
src/main/kotlin/controller/api/TagApiController.kt
src/main/kotlin/service/TagService.kt
Ignore Space
Show notes
View
src/main/kotlin/controller/api/TagApiController.kt
/* * Copyright (c) 2023. yo-saito. All Rights Reserved. */ package net.piedpiper.bremer.controller.api import jakarta.validation.Valid import net.piedpiper.bremer.model.api.TagAudioRequest import net.piedpiper.bremer.model.api.TagAudioResponse import net.piedpiper.bremer.model.api.TagRequest import net.piedpiper.bremer.model.api.AllTagsResponse import net.piedpiper.bremer.service.TagService import org.springframework.beans.factory.annotation.Qualifier import org.springframework.http.ResponseEntity import org.springframework.web.bind.annotation.* @RestController("bremer.controller.TagApiController") @RequestMapping("bremer/api/tag") class TagApiController( @Qualifier("bremer.service.TagService") private val tagService: TagService ) { @GetMapping fun getAllTags(): ResponseEntity<AllTagsResponse> = ResponseEntity.ok(tagService.getAllTags()) @PostMapping fun createTag(@Valid @RequestBody request: TagRequest): ResponseEntity<Void> { tagService.createTag(request) return ResponseEntity.ok(null) } @GetMapping("{slug}") fun getAudioList(@PathVariable("slug") slug: String): ResponseEntity<TagAudioResponse> = ResponseEntity.ok(tagService.getAudioListByTagSlug(slug)) @PutMapping("{slug}") fun updateTag( @PathVariable("slug") slug: String, @Valid @RequestBody request: TagRequest ): ResponseEntity<Void> { tagService.updateTag(slug, request) return ResponseEntity.ok(null) } @DeleteMapping("{slug}") fun deleteTag( @PathVariable("slug") slug: String ): ResponseEntity<Void> { tagService.deleteTag(slug) return ResponseEntity.ok(null) } @PostMapping("audio/{slug}") fun addAudioList( @PathVariable("slug") slug: String, @Valid @RequestBody request: TagAudioRequest ): ResponseEntity<Void> { tagService.addTagToAudioList(slug, request) return ResponseEntity.ok(null) } @DeleteMapping("audio/{slug}") fun deleteAudioList( @PathVariable("slug") slug: String, @Valid @RequestBody request: TagAudioRequest ): ResponseEntity<Void> { tagService.deleteTagFromAudioList(slug, request) return ResponseEntity.ok(null) } }
/* * Copyright (c) 2023. yo-saito. All Rights Reserved. */ package net.piedpiper.bremer.controller.api import jakarta.validation.Valid import net.piedpiper.bremer.model.api.TagAudioRequest import net.piedpiper.bremer.model.api.TagAudioResponse import net.piedpiper.bremer.model.api.TagRequest import net.piedpiper.bremer.model.api.AllTagsResponse import net.piedpiper.bremer.service.TagService import org.springframework.beans.factory.annotation.Qualifier import org.springframework.http.ResponseEntity import org.springframework.web.bind.annotation.* @RestController("bremer.controller.TagApiController") @RequestMapping("bremer/api/tag") class TagApiController( @Qualifier("bremer.service.TagService") private val tagService: TagService ) { @GetMapping fun getAllTags(): ResponseEntity<AllTagsResponse> = ResponseEntity.ok(tagService.getAllTags()) @PostMapping fun createTag(@Valid @RequestBody request: TagRequest): ResponseEntity<Void> { tagService.createTag(request) return ResponseEntity.ok(null) } @GetMapping("{slug}") fun getAudioList(@PathVariable("slug") slug: String): ResponseEntity<TagAudioResponse> = ResponseEntity.ok(tagService.getAudioListByTagSlug(slug)) @PutMapping("{slug}") fun updateTag( @PathVariable("slug") slug: String, @Valid @RequestBody request: TagRequest ): ResponseEntity<Void> { tagService.updateTag(slug, request) return ResponseEntity.ok(null) } @PostMapping("audio/{slug}") fun addAudioList( @PathVariable("slug") slug: String, @Valid @RequestBody request: TagAudioRequest ): ResponseEntity<Void> { tagService.addTagToAudioList(slug, request) return ResponseEntity.ok(null) } @DeleteMapping("audio/{slug}") fun deleteAudioList( @PathVariable("slug") slug: String, @Valid @RequestBody request: TagAudioRequest ): ResponseEntity<Void> { tagService.deleteTagFromAudioList(slug, request) return ResponseEntity.ok(null) } }
Ignore Space
Show notes
View
src/main/kotlin/service/TagService.kt
/* * Copyright (c) 2023. yo-saito. All Rights Reserved. */ package net.piedpiper.bremer.service import net.piedpiper.bremer.dao.AudioDao import net.piedpiper.bremer.dao.AudioTagDao import net.piedpiper.bremer.dao.TagDao import net.piedpiper.bremer.entity.AudioTagEntity import net.piedpiper.bremer.entity.TagEntity import net.piedpiper.bremer.exception.NotFoundException import net.piedpiper.bremer.model.api.TagAudioRequest import net.piedpiper.bremer.model.api.TagAudioResponse import net.piedpiper.bremer.model.api.TagRequest import net.piedpiper.bremer.model.api.AllTagsResponse import org.springframework.beans.factory.annotation.Qualifier import org.springframework.stereotype.Service import java.util.* @Service("bremer.service.TagService") class TagService( @Qualifier("bremer.dao.AudioDao") private val audioDao: AudioDao, @Qualifier("bremer.dao.AudioTagDao") private val audioTagDao: AudioTagDao, @Qualifier("bremer.dao.TagDao") private val tagDao: TagDao ) { fun getAllTags(): AllTagsResponse = AllTagsResponse(tagDao.findAll()) fun createTag(request: TagRequest) { tagDao.insertOne(TagEntity(slug = UUID.randomUUID().toString(), name = request.name)) } fun updateTag(tagSlug: String, request: TagRequest) { tagDao.findOneBySlugWithLock(tagSlug) ?.let { it.name = request.name tagDao.updateOne(it) } ?: throw NotFoundException() } fun deleteTag(slug: String) = tagDao.findOneBySlugWithLock(slug) ?.let { tagDao.deleteById(it.id) } ?: throw NotFoundException() fun getAudioListByTagSlug(tagSlug: String): TagAudioResponse = tagDao.findOneBySlug(tagSlug) ?.let { tag -> TagAudioResponse( tag, audioDao.findAllByTagIdIn(listOf(tag.id)) ) } ?: throw NotFoundException() fun addTagToAudioList(tagSlug: String, request: TagAudioRequest) { tagDao.findOneBySlug(tagSlug) ?.let { tag -> audioDao.findAllBySlugIn(request.audioSlugs) .forEach { audio -> if (audioTagDao.findByAudioIdAndTagId(audio.id, tag.id) != null) { return@forEach } audioTagDao.insertOne( AudioTagEntity( audioId = audio.id, tagId = tag.id ) ) } } ?: throw NotFoundException() } fun deleteTagFromAudioList(tagSlug: String, request: TagAudioRequest) { tagDao.findOneBySlug(tagSlug) ?.let { tag -> audioDao.findAllBySlugIn(request.audioSlugs) .forEach { audio -> audioTagDao.deleteOneByAudioIdAndTagId(audio.id, tag.id) } } ?: throw NotFoundException() } }
/* * Copyright (c) 2023. yo-saito. All Rights Reserved. */ package net.piedpiper.bremer.service import net.piedpiper.bremer.dao.AudioDao import net.piedpiper.bremer.dao.AudioTagDao import net.piedpiper.bremer.dao.TagDao import net.piedpiper.bremer.entity.AudioTagEntity import net.piedpiper.bremer.entity.TagEntity import net.piedpiper.bremer.exception.NotFoundException import net.piedpiper.bremer.model.api.TagAudioRequest import net.piedpiper.bremer.model.api.TagAudioResponse import net.piedpiper.bremer.model.api.TagRequest import net.piedpiper.bremer.model.api.AllTagsResponse import org.springframework.beans.factory.annotation.Qualifier import org.springframework.stereotype.Service import java.util.* @Service("bremer.service.TagService") class TagService( @Qualifier("bremer.dao.AudioDao") private val audioDao: AudioDao, @Qualifier("bremer.dao.AudioTagDao") private val audioTagDao: AudioTagDao, @Qualifier("bremer.dao.TagDao") private val tagDao: TagDao ) { fun getAllTags(): AllTagsResponse = AllTagsResponse(tagDao.findAll()) fun createTag(request: TagRequest) { tagDao.insertOne(TagEntity(slug = UUID.randomUUID().toString(), name = request.name)) } fun updateTag(tagSlug: String, request: TagRequest) { tagDao.findOneBySlugWithLock(tagSlug) ?.let { it.name = request.name tagDao.updateOne(it) } ?: throw NotFoundException() } fun getAudioListByTagSlug(tagSlug: String): TagAudioResponse = tagDao.findOneBySlug(tagSlug) ?.let { tag -> TagAudioResponse( tag, audioDao.findAllByTagIdIn(listOf(tag.id)) ) } ?: throw NotFoundException() fun addTagToAudioList(tagSlug: String, request: TagAudioRequest) { tagDao.findOneBySlug(tagSlug) ?.let { tag -> audioDao.findAllBySlugIn(request.audioSlugs) .forEach { audio -> if (audioTagDao.findByAudioIdAndTagId(audio.id, tag.id) != null) { return@forEach } audioTagDao.insertOne( AudioTagEntity( audioId = audio.id, tagId = tag.id ) ) } } ?: throw NotFoundException() } fun deleteTagFromAudioList(tagSlug: String, request: TagAudioRequest) { tagDao.findOneBySlug(tagSlug) ?.let { tag -> audioDao.findAllBySlugIn(request.audioSlugs) .forEach { audio -> audioTagDao.deleteOneByAudioIdAndTagId(audio.id, tag.id) } } ?: throw NotFoundException() } }
Show line notes below