Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial version of Technologies Entity #213

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package pt.up.fe.ni.website.backend.controller

import org.springframework.web.bind.annotation.*
import pt.up.fe.ni.website.backend.dto.entity.TechnologyDto
import pt.up.fe.ni.website.backend.service.TechnologyService

@RestController
@RequestMapping("/technologies")
class TechnologyController(private val technologyService: TechnologyService) {
@GetMapping
fun getAllTechnologies() = technologyService.getAllTechnologies()

@GetMapping("/{id}")
fun getTechnology(@PathVariable id: Long) = technologyService.getTechnologyById(id)

@PostMapping
fun createNewTechnology(@RequestBody dto: TechnologyDto) = technologyService.createTechnology(dto)

@DeleteMapping("/{id}")
fun deleteTechnology(@PathVariable id: Long) = technologyService.deleteTechnology(id)

@PutMapping("/{id}")
fun updateTechnology(@PathVariable id: Long, @RequestBody dto: TechnologyDto) = technologyService.updateTechnology(id, dto)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package pt.up.fe.ni.website.backend.dto.entity

import pt.up.fe.ni.website.backend.model.Technology

class TechnologyDto(
val name: String,
val logo: String?,
) : EntityDto<Technology>()
21 changes: 21 additions & 0 deletions src/main/kotlin/pt/up/fe/ni/website/backend/model/Technology.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package pt.up.fe.ni.website.backend.model

import com.fasterxml.jackson.annotation.JsonProperty
import jakarta.persistence.Entity
import jakarta.persistence.GeneratedValue
import jakarta.persistence.Id
import jakarta.validation.constraints.NotBlank

@Entity
class Technology(
@JsonProperty(required = true)
@field:NotBlank
var name: String,

@JsonProperty(required = true)
@field:NotBlank
var logo: String,

@Id @GeneratedValue
val id: Long? = null
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package pt.up.fe.ni.website.backend.repository

import org.springframework.data.repository.CrudRepository
import org.springframework.stereotype.Repository
import pt.up.fe.ni.website.backend.model.Technology

@Repository
interface TechnologyRepository : CrudRepository<Technology, Long>
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,6 @@ object ErrorMessages {
fun userAlreadyHasRole(roleId: Long, userId: Long): String = "user $userId already has role $roleId"

fun userNotInRole(roleId: Long, userId: Long): String = "user $userId doesn't have role $roleId"

fun technologyNotFound(id: Long): String = "technology not found with id $id"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package pt.up.fe.ni.website.backend.service

import org.springframework.stereotype.Service
import pt.up.fe.ni.website.backend.dto.entity.TechnologyDto
import pt.up.fe.ni.website.backend.model.Technology
import pt.up.fe.ni.website.backend.repository.TechnologyRepository

@Service
class TechnologyService(
private val technologyRepository: TechnologyRepository,
) {
fun getTechnologyById(technologyId: Long): Technology {
val technology = technologyRepository.findById(technologyId).orElseThrow {
throw NoSuchElementException(ErrorMessages.technologyNotFound(technologyId))
}
return technology
}

fun getAllTechnologies() : List<Technology> = technologyRepository.findAll().toList()

fun createTechnology(dto: TechnologyDto): Technology {
val technology = dto.create()
return technologyRepository.save(technology)
}

fun updateTechnology(technologyId: Long, dto: TechnologyDto): Technology {
val technology = getTechnologyById(technologyId)
dto.update(technology)
return technologyRepository.save(technology)
}

fun deleteTechnology(technologyId: Long) {
val technology = getTechnologyById(technologyId)
technologyRepository.delete(technology)
}
}
Loading