Skip to content

Commit

Permalink
Adds creation post request
Browse files Browse the repository at this point in the history
  • Loading branch information
jesperancinha committed Jan 31, 2024
1 parent bb2a249 commit 4bfd915
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 6 deletions.
12 changes: 12 additions & 0 deletions repeated-dislikes/generated-requests.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
###
POST http://localhost:8080/fix/users
Content-Type: application/json

{
"name": "Joao",
"receipts": [],
"shops": []
}

###
GET http://localhost:8080/fix/users/all
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ data class ShopDTO(
)


fun User.toDto() = UserDTO (
fun User.toDto() = UserDTO(
id = id,
name = name,
receipts = receipts.map { it.id },
shops = shops.map { it.name }
)
)
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package org.jesperancinha.repeateddislikes.badfix.domain

import jakarta.persistence.*
import java.util.UUID
import kotlin.random.Random

const val SCHEMA_BAD = "BADFIX"

Expand Down Expand Up @@ -31,6 +32,7 @@ data class User(
val shops: Set<Shop>
)


@Table(name = "receipts", schema = SCHEMA_BAD)
@Entity(name = "badfix_receipt")
data class Receipt(
Expand Down Expand Up @@ -60,3 +62,4 @@ data class Shop(
)
val receipts: List<Receipt>
)

Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ fun User.toDto() = UserDTO (
name = name,
receipts = receipts.map { it.id },
shops = shops.map { it.name }
)
)
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package org.jesperancinha.repeateddislikes.fix.controllers

import org.jesperancinha.repeateddislikes.fix.dtos.UserDTO
import org.jesperancinha.repeateddislikes.fix.services.DislikeService
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

Expand All @@ -19,4 +22,7 @@ class DislikesController(

@GetMapping("shops/all")
fun getAllShops() = dislikeService.getAllShops()

@PostMapping("users")
fun createUser(@RequestBody userDTO: UserDTO) = dislikeService.create(userDTO)
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const val SCHEMA = "fix"
data class User(
@Id
@GeneratedValue(strategy = GenerationType.UUID)
val id: UUID,
val id: UUID? = UUID.randomUUID(),
val name: String,
@OneToMany(cascade = [CascadeType.DETACH], fetch = FetchType.EAGER)
@JoinTable(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import org.jesperancinha.repeateddislikes.fix.domain.User
import java.util.*

data class UserDTO(
val id: UUID,
val id: UUID?,
val name: String,
val receipts: List<UUID>,
val shops: List<String>
Expand All @@ -29,4 +29,11 @@ fun User.toDto() = UserDTO (
name = name,
receipts = receipts.map { it.id },
shops = shops.map { it.name }
)
)

fun UserDTO.toEntity() = User(
id = id,
name = name,
receipts = emptyList(),
shops = emptyList()
)
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package org.jesperancinha.repeateddislikes.fix.services
import org.jesperancinha.repeateddislikes.fix.dao.ReceiptRepository
import org.jesperancinha.repeateddislikes.fix.dao.ShopRepository
import org.jesperancinha.repeateddislikes.fix.dao.UserRepository
import org.jesperancinha.repeateddislikes.fix.dtos.UserDTO
import org.jesperancinha.repeateddislikes.fix.dtos.toDto
import org.jesperancinha.repeateddislikes.fix.dtos.toEntity
import org.springframework.stereotype.Service

@Service
Expand All @@ -17,4 +19,6 @@ class DislikeService(
fun getAllReceipts() = receiptRepository.findAll()

fun getAllShops() = shopRepository.findAll()

fun create(userDTO: UserDTO) = userRepository.save(userDTO.toEntity()).toDto()
}

0 comments on commit 4bfd915

Please sign in to comment.