Skip to content

Commit

Permalink
Finishes the wrong example
Browse files Browse the repository at this point in the history
  • Loading branch information
jesperancinha committed Jan 30, 2024
1 parent 11edc18 commit 33e5f6c
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 12 deletions.
Expand Up @@ -14,8 +14,8 @@ class BadDislikesController(
@GetMapping("users/all")
fun getAllUsers() = dislikeService.getAllUsers()

@GetMapping("reports/all")
fun getAllReports() = dislikeService.getAllReports()
@GetMapping("receipts/all")
fun getAllReceipts() = dislikeService.getAllReceipts()

@GetMapping("shops/all")
fun getAllShops() = dislikeService.getAllShops()
Expand Down
Expand Up @@ -13,20 +13,20 @@ data class User(
@GeneratedValue(strategy = GenerationType.UUID)
val id: UUID,
val name: String,
@ManyToMany(cascade = [CascadeType.DETACH], fetch = FetchType.EAGER)
@ManyToMany(cascade = [CascadeType.DETACH], fetch = FetchType.LAZY)
@JoinTable(
schema = SCHEMA_BAD,
name = "bad_dislikes_relations",
joinColumns = [JoinColumn(name= "user_id")],
inverseJoinColumns = [JoinColumn(name="receipt_id")]
)
val receipts: List<Receipt>,
@ManyToMany(cascade = [CascadeType.DETACH], fetch = FetchType.EAGER)
@ManyToMany(cascade = [CascadeType.DETACH], fetch = FetchType.LAZY)
@JoinTable(
schema = SCHEMA_BAD,
name = "bad_dislikes_relations",
joinColumns = [JoinColumn(name= "user_id")],
inverseJoinColumns = [JoinColumn(name="receipt_id")]
inverseJoinColumns = [JoinColumn(name="shop_id")]
)
val shops: List<Shop>
)
Expand All @@ -44,14 +44,15 @@ data class Receipt(
)



@Table(name = "shops", schema = SCHEMA_BAD)
@Entity
data class Shop(
@Id
@GeneratedValue(strategy = GenerationType.UUID)
val id: UUID,
val name: String,
@ManyToMany(cascade = [CascadeType.DETACH], fetch = FetchType.EAGER)
@ManyToMany(cascade = [CascadeType.DETACH], fetch = FetchType.LAZY)
@JoinTable(
schema = SCHEMA_BAD,
name = "bad_dislikes_relations",
Expand Down
@@ -0,0 +1,32 @@
package org.jesperancinha.repeateddislikes.bad.dtos

import org.jesperancinha.repeateddislikes.bad.domain.User
import java.util.*

data class UserDTO(
val id: UUID,
val name: String,
val receipts: List<UUID>,
val shops: List<String>
)

data class ReceiptDTO(
val id: UUID,
val user: UserDTO,
val shop: ShopDTO
)


data class ShopDTO(
val id: UUID,
val name: String,
val receipts: List<ReceiptDTO>
)


fun User.toDto() = UserDTO (
id = id,
name = name,
receipts = receipts.map { it.id },
shops = shops.map { it.name }
)
Expand Up @@ -3,6 +3,7 @@ package org.jesperancinha.repeateddislikes.bad.services
import org.jesperancinha.repeateddislikes.bad.dao.ReceiptRepository
import org.jesperancinha.repeateddislikes.bad.dao.ShopRepository
import org.jesperancinha.repeateddislikes.bad.dao.UserRepository
import org.jesperancinha.repeateddislikes.bad.dtos.toDto
import org.springframework.stereotype.Service

@Service
Expand All @@ -11,9 +12,9 @@ class DislikeService(
val receiptRepository: ReceiptRepository,
val shopRepository: ShopRepository
) {
fun getAllUsers() = userRepository.findAll()
fun getAllUsers() = userRepository.findAll().map { it.toDto() }

fun getAllReports() = receiptRepository.findAll()
fun getAllReceipts() = receiptRepository.findAll()

fun getAllShops() = shopRepository.findAll()
}
25 changes: 24 additions & 1 deletion repeated-dislikes/src/main/resources/data.sql
@@ -1 +1,24 @@
SELECT LOG(2);
insert into BAD.USERS (ID, NAME)
values (random_uuid(), 'Liking Cat 1');
insert into BAD.SHOPS (ID, NAME)
values (random_uuid(), 'Cats Para Dice');
SET @receipt1=random_uuid();
SET @receipt2=random_uuid();
insert into BAD.RECEIPTS (ID, USER_ID, SHOP_ID)
values (@receipt1,
(select ID from BAD.USERS WHERE NAME = 'Liking Cat 1'),
(select ID from BAD.SHOPS WHERE NAME = 'Cats Para Dice')
);
insert into BAD.BAD_DISLIKES_RELATIONS (ID, USER_ID, SHOP_ID, RECEIPT_ID)
values (random_uuid(),
(select ID from BAD.USERS WHERE NAME = 'Liking Cat 1'),
(select ID from BAD.SHOPS WHERE NAME = 'Cats Para Dice'),
@receipt1
);

insert into BAD.BAD_DISLIKES_RELATIONS (ID, USER_ID, SHOP_ID, RECEIPT_ID)
values (random_uuid(),
(select ID from BAD.USERS WHERE NAME = 'Liking Cat 1'),
(select ID from BAD.SHOPS WHERE NAME = 'Cats Para Dice'),
@receipt2
);
6 changes: 3 additions & 3 deletions repeated-dislikes/src/main/resources/schema.sql
Expand Up @@ -11,9 +11,9 @@ CREATE SCHEMA IF NOT EXISTS BAD;
CREATE TABLE IF NOT EXISTS BAD.BAD_DISLIKES_RELATIONS
(
id UUID NOT NULL DEFAULT random_uuid() PRIMARY KEY,
user_id INT NOT NULL,
shop_id INT NOT NULL,
receipt_id INT NOT NULL
user_id UUID NOT NULL,
shop_id UUID NOT NULL,
receipt_id UUID NOT NULL
);

CREATE TABLE IF NOT EXISTS BAD.USERS
Expand Down

0 comments on commit 33e5f6c

Please sign in to comment.