Adds code
This commit is contained in:
11
src/main/kotlin/net/halfbinary/tmnt/TmntApplication.kt
Normal file
11
src/main/kotlin/net/halfbinary/tmnt/TmntApplication.kt
Normal file
@@ -0,0 +1,11 @@
|
||||
package net.halfbinary.tmnt
|
||||
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication
|
||||
import org.springframework.boot.runApplication
|
||||
|
||||
@SpringBootApplication
|
||||
class TmntApplication
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
runApplication<TmntApplication>(*args)
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package net.halfbinary.tmnt.controller
|
||||
|
||||
import net.halfbinary.tmnt.dto.MovieRequest
|
||||
import net.halfbinary.tmnt.dto.MovieResponse
|
||||
import net.halfbinary.tmnt.service.MoviesService
|
||||
import org.springframework.web.bind.annotation.CrossOrigin
|
||||
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
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/movies")
|
||||
@CrossOrigin(origins = ["http://localhost:5173"])
|
||||
class MoviesController(val moviesService: MoviesService) {
|
||||
@GetMapping
|
||||
fun getAllMovies(): List<MovieResponse> {
|
||||
return moviesService.getAllMovies().map { MovieResponse.fromModel(it) }
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
fun addMovie(@RequestBody body: MovieRequest) {
|
||||
moviesService.createNewMovie(body.toModel())
|
||||
}
|
||||
|
||||
}
|
||||
11
src/main/kotlin/net/halfbinary/tmnt/dto/MovieRequest.kt
Normal file
11
src/main/kotlin/net/halfbinary/tmnt/dto/MovieRequest.kt
Normal file
@@ -0,0 +1,11 @@
|
||||
package net.halfbinary.tmnt.dto
|
||||
|
||||
import net.halfbinary.tmnt.model.Movie
|
||||
import java.time.LocalDateTime
|
||||
import java.util.*
|
||||
|
||||
data class MovieRequest(val title: String, val screenedDate: LocalDateTime, val releaseYear: Int, val imdbLink: String, val notes: String) {
|
||||
fun toModel(): Movie {
|
||||
return Movie(UUID.randomUUID(), this.title, this.screenedDate, this.releaseYear, this.imdbLink, this.notes)
|
||||
}
|
||||
}
|
||||
13
src/main/kotlin/net/halfbinary/tmnt/dto/MovieResponse.kt
Normal file
13
src/main/kotlin/net/halfbinary/tmnt/dto/MovieResponse.kt
Normal file
@@ -0,0 +1,13 @@
|
||||
package net.halfbinary.tmnt.dto
|
||||
|
||||
import net.halfbinary.tmnt.model.Movie
|
||||
import java.time.LocalDateTime
|
||||
import java.util.*
|
||||
|
||||
data class MovieResponse(val id: UUID, val title: String, val screenedDate: LocalDateTime, val releaseYear: Int, val imdbLink: String, val notes: String) {
|
||||
companion object {
|
||||
fun fromModel(movie: Movie): MovieResponse {
|
||||
return MovieResponse(movie.id, movie.title, movie.screenedDate, movie.releaseYear, movie.imdbLink, movie.notes)
|
||||
}
|
||||
}
|
||||
}
|
||||
6
src/main/kotlin/net/halfbinary/tmnt/model/Movie.kt
Normal file
6
src/main/kotlin/net/halfbinary/tmnt/model/Movie.kt
Normal file
@@ -0,0 +1,6 @@
|
||||
package net.halfbinary.tmnt.model
|
||||
|
||||
import java.time.LocalDateTime
|
||||
import java.util.*
|
||||
|
||||
data class Movie(val id: UUID = UUID.randomUUID(), val title: String, val screenedDate: LocalDateTime, val releaseYear: Int, val imdbLink: String, val notes: String)
|
||||
@@ -0,0 +1,28 @@
|
||||
package net.halfbinary.tmnt.repository
|
||||
|
||||
import jakarta.persistence.Entity
|
||||
import jakarta.persistence.Id
|
||||
import jakarta.persistence.Table
|
||||
import net.halfbinary.tmnt.model.Movie
|
||||
import org.springframework.data.jpa.repository.JpaRepository
|
||||
import org.springframework.stereotype.Repository
|
||||
import java.time.LocalDateTime
|
||||
import java.util.*
|
||||
|
||||
@Repository
|
||||
interface MoviesRepository: JpaRepository<MovieEntity, UUID> {
|
||||
}
|
||||
|
||||
@Entity
|
||||
@Table(name = "movies")
|
||||
data class MovieEntity(@Id val id: UUID, val title: String, val screenedDate: LocalDateTime, val releaseYear: Int, val imdbLink: String, val notes: String) {
|
||||
fun toModel(): Movie {
|
||||
return Movie(this.id, this.title, this.screenedDate, this.releaseYear, this.imdbLink, this.notes)
|
||||
}
|
||||
|
||||
companion object {
|
||||
fun fromModel(movie: Movie): MovieEntity {
|
||||
return MovieEntity(movie.id, movie.title, movie.screenedDate, movie.releaseYear, movie.imdbLink, movie.notes)
|
||||
}
|
||||
}
|
||||
}
|
||||
17
src/main/kotlin/net/halfbinary/tmnt/service/MoviesService.kt
Normal file
17
src/main/kotlin/net/halfbinary/tmnt/service/MoviesService.kt
Normal file
@@ -0,0 +1,17 @@
|
||||
package net.halfbinary.tmnt.service
|
||||
|
||||
import net.halfbinary.tmnt.model.Movie
|
||||
import net.halfbinary.tmnt.repository.MovieEntity
|
||||
import net.halfbinary.tmnt.repository.MoviesRepository
|
||||
import org.springframework.stereotype.Service
|
||||
|
||||
@Service
|
||||
class MoviesService(val moviesRepository: MoviesRepository) {
|
||||
fun getAllMovies(): List<Movie> {
|
||||
return moviesRepository.findAll().map { it.toModel()}
|
||||
}
|
||||
|
||||
fun createNewMovie(newMovie: Movie) {
|
||||
moviesRepository.save(MovieEntity.fromModel(newMovie))
|
||||
}
|
||||
}
|
||||
16
src/main/resources/add movie.http
Normal file
16
src/main/resources/add movie.http
Normal file
@@ -0,0 +1,16 @@
|
||||
GET http://localhost:8080/movies
|
||||
Accept: application/json
|
||||
|
||||
###
|
||||
POST http://localhost:8080/movies
|
||||
Content-Type: application/json
|
||||
|
||||
{
|
||||
"title":"The Addams Family",
|
||||
"screenedDate": "2024-02-18T20:30:00.000",
|
||||
"releaseYear": 1991,
|
||||
"imdbLink": "https://www.imdb.com/title/tt0101272",
|
||||
"notes": ""
|
||||
}
|
||||
|
||||
###
|
||||
7
src/main/resources/application.properties
Normal file
7
src/main/resources/application.properties
Normal file
@@ -0,0 +1,7 @@
|
||||
spring.application.name=tmnt
|
||||
spring.jpa.hibernate.ddl-auto=update
|
||||
spring.datasource.url=jdbc:mysql://192.168.187.129:3307/tmnt
|
||||
spring.datasource.driverClassName=com.mysql.jdbc.Driver
|
||||
#spring.jpa.database-platform=org.hibernate.dialect.mysql
|
||||
spring.datasource.username=tmntdb
|
||||
spring.datasource.password=1mstr.SPLNTR
|
||||
5
src/main/resources/http-client.env.json
Normal file
5
src/main/resources/http-client.env.json
Normal file
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"dev": {
|
||||
"name": "value"
|
||||
}
|
||||
}
|
||||
2
src/main/resources/initdata.sql
Normal file
2
src/main/resources/initdata.sql
Normal file
@@ -0,0 +1,2 @@
|
||||
INSERT INTO movies (id, title, screened_date) VALUES
|
||||
('b621badb-898c-4ebf-b165-a4cb52cb2234', 'Test 1', '2023-01-15 00:00:00.0000')
|
||||
13
src/test/kotlin/net/halfbinary/tmnt/TmntApplicationTests.kt
Normal file
13
src/test/kotlin/net/halfbinary/tmnt/TmntApplicationTests.kt
Normal file
@@ -0,0 +1,13 @@
|
||||
package net.halfbinary.tmnt
|
||||
|
||||
import org.junit.jupiter.api.Test
|
||||
import org.springframework.boot.test.context.SpringBootTest
|
||||
|
||||
@SpringBootTest
|
||||
class TmntApplicationTests {
|
||||
|
||||
@Test
|
||||
fun contextLoads() {
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user