Skip to content

Commit

Permalink
#134 Extras are now item-specific
Browse files Browse the repository at this point in the history
  • Loading branch information
kovacsbertalan committed Mar 16, 2022
1 parent 07c3df6 commit aec98bd
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 13 deletions.
1 change: 1 addition & 0 deletions src/main/kotlin/hu/kirdev/schpincer/dao/Repositories.kt
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,5 @@ interface UserRepository : JpaRepository<UserEntity, String> {
@Repository
interface ExtrasRepository : JpaRepository<ExtraEntity, Long> {
fun findByItemAndNameAndInputTypeAndSelectedIndex(item: ItemEntity, name: String, inputType: CustomComponentType, selectedIndex: Int): Optional<ExtraEntity>
fun findByItemAndNameAndInputTypeAndSelectedIndexAndActiveTrue(item: ItemEntity, name: String, inputType: CustomComponentType, selectedIndex: Int): Optional<ExtraEntity>
}
19 changes: 10 additions & 9 deletions src/main/kotlin/hu/kirdev/schpincer/model/ExtraEntity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,29 @@ data class ExtraEntity(
@GeneratedValue(strategy = GenerationType.AUTO)
var id: Long = 0,

@ManyToOne
var circle: CircleEntity,
@Column
val category: String,

@Column
var category: String,
val selectedIndex: Int,

@Column
var selectedIndex: Int,
val displayName: String,

@Column
var displayName: String,
val inputType: CustomComponentType,

@Column
var inputType: CustomComponentType,
val name: String,

@Column
var name: String,
val price: Int,

@Column
var price: Int) {
var active: Boolean? = true) {

@ManyToOne
lateinit var item: ItemEntity
@JoinColumn(name = "itemId", columnDefinition = "DEFAULT NULL")
var item: ItemEntity? = null

}
24 changes: 21 additions & 3 deletions src/main/kotlin/hu/kirdev/schpincer/service/ExtrasService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,22 @@ class ExtrasService {
i
)
optionalExtra.ifPresentOrElse({
it.price = mappedExtra.prices[i]
extrasRepository.save(it)
if (it.price == mappedExtra.prices[i] &&
it.displayName == mappedExtra.values[i]) {
return@ifPresentOrElse;
}
val newExtra = ExtraEntity(
category = mappedExtra.aliases.getOrElse(i) { i.toString() },
selectedIndex = i,
inputType = CustomComponentType.valueOf(mappedExtra.type),
name = mappedExtra.name,
displayName = mappedExtra.values[i],
price = mappedExtra.prices[i]
)
it.active = false
extrasRepository.saveAll(listOf(newExtra, it))
}) {
val extra = ExtraEntity(
circle = circle,
category = mappedExtra.aliases.getOrElse(i) { i.toString() },
selectedIndex = i,
inputType = CustomComponentType.valueOf(mappedExtra.type),
Expand All @@ -79,6 +90,13 @@ class ExtrasService {
@PostConstruct
fun generateAllExtrasForAllCircles() {

extrasRepository.findAll().let {
it.map {
it.active = it.active ?: true
}
extrasRepository.saveAll(it)
}

for (circle in circleRepository.findAll()) {
createExtrasForCircle(circle)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ class MakeOrderProcedure (
val name = answer.name
for (selected in answer.selected) {

val optionalExtra = extrasRepository.findByItemAndNameAndInputTypeAndSelectedIndex(item, name, type, selected)
val optionalExtra = extrasRepository.findByItemAndNameAndInputTypeAndSelectedIndexAndActiveTrue(item, name, type, selected)
if (optionalExtra.isEmpty) {
throw IllegalArgumentException("${current.circle!!} has no optional extra with input type $type and name $name")
}
Expand Down

0 comments on commit aec98bd

Please sign in to comment.