note author specified
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run
Details
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run
Details
This commit is contained in:
parent
7d0d60d72f
commit
5f4bc4405d
|
|
@ -1,9 +0,0 @@
|
|||
on:
|
||||
pull_request:
|
||||
branches: [ master ]
|
||||
push:
|
||||
branches: [ master ]
|
||||
|
||||
jobs:
|
||||
steps:
|
||||
run: pwd
|
||||
|
|
@ -3,7 +3,12 @@ package com.kassaev.notes.controller.note
|
|||
import com.kassaev.notes.model.Note
|
||||
import com.kassaev.notes.service.NoteService
|
||||
import lombok.AllArgsConstructor
|
||||
import org.springframework.http.HttpStatus
|
||||
import org.springframework.http.HttpStatusCode
|
||||
import org.springframework.security.core.context.SecurityContextHolder
|
||||
import org.springframework.web.bind.annotation.*
|
||||
import org.springframework.web.server.ResponseStatusException
|
||||
import java.security.Principal
|
||||
import java.util.*
|
||||
|
||||
@RestController
|
||||
|
|
@ -12,27 +17,46 @@ import java.util.*
|
|||
class NoteController(val service: NoteService) {
|
||||
|
||||
@GetMapping("/all")
|
||||
fun getAllNotes(): List<Note> {
|
||||
return service.getAllNotes()
|
||||
fun getAllNotes(principal: Principal): List<Note> {
|
||||
// val userEmail = SecurityContextHolder.getContext().authentication.name
|
||||
return service.getAllNotes(principal.name)
|
||||
}
|
||||
|
||||
@PostMapping("/create")
|
||||
fun createNote(@RequestBody note: Note): Note {
|
||||
return service.updateNote(note)
|
||||
fun createNote(@RequestBody note: Note, principal: Principal): Note? {
|
||||
return service.createNote(
|
||||
note = note.copy(author = principal.name)
|
||||
)
|
||||
}
|
||||
@PutMapping("/update")
|
||||
fun updateNote(@RequestBody note: Note, principal: Principal): HttpStatusCode {
|
||||
val resCode = note.id?.let {
|
||||
service.updateNote(
|
||||
noteText = note.text,
|
||||
author = principal.name,
|
||||
noteId = it
|
||||
)
|
||||
}
|
||||
if (resCode == 1){
|
||||
return HttpStatus.OK
|
||||
}
|
||||
throw ResponseStatusException(HttpStatus.BAD_REQUEST, "Cannot update note.")
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
fun getNoteById(@PathVariable id: Long): Optional<Note> {
|
||||
return service.getNoteById(id)
|
||||
fun getNoteById(@PathVariable id: Long, principal: Principal): Note? {
|
||||
return service.getNoteByIdAndAuthor(
|
||||
id = id,
|
||||
author = principal.name
|
||||
)
|
||||
}
|
||||
|
||||
@PutMapping("/update")
|
||||
fun updateNote(@RequestBody note: Note): Note {
|
||||
return service.updateNote(note)
|
||||
}
|
||||
|
||||
@DeleteMapping("/remove/{id}")
|
||||
fun deleteNote(@PathVariable id: Long){
|
||||
return service.deleteNote(id)
|
||||
fun deleteNote(@PathVariable id: Long, principal: Principal): HttpStatus {
|
||||
return service.deleteNote(
|
||||
id = id,
|
||||
author = principal.name
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
@ -9,5 +9,6 @@ data class Note(
|
|||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
val id: Long?,
|
||||
val text: String,
|
||||
val dateCreated: String
|
||||
val dateCreated: String,
|
||||
val author: String
|
||||
)
|
||||
|
|
@ -1,8 +1,23 @@
|
|||
package com.kassaev.notes.repository
|
||||
|
||||
import com.kassaev.notes.model.Note
|
||||
//import jakarta.transaction.Transactional
|
||||
import org.springframework.data.jpa.repository.JpaRepository
|
||||
import org.springframework.data.jpa.repository.Modifying
|
||||
import org.springframework.data.jpa.repository.Query
|
||||
import org.springframework.data.repository.query.Param
|
||||
import org.springframework.transaction.annotation.Transactional
|
||||
|
||||
interface INoteRepository: JpaRepository<Note, Long> {
|
||||
|
||||
@Query("SELECT * FROM notes WHERE author = :email", nativeQuery = true)
|
||||
fun findAll(@Param("email") email: String): List<Note>
|
||||
|
||||
fun findNotesByAuthor(author: String): List<Note>
|
||||
@Modifying
|
||||
@Transactional
|
||||
@Query("UPDATE notes SET text=:noteText WHERE author = :author and id = :noteId", nativeQuery = true)
|
||||
fun updateNote(noteText: String, author: String, noteId: Long): Int
|
||||
|
||||
fun getNoteByIdAndAuthor(id: Long, author: String): Note?
|
||||
}
|
||||
|
|
@ -6,6 +6,7 @@ import org.springframework.data.jpa.repository.Query
|
|||
import org.springframework.data.repository.query.Param
|
||||
|
||||
interface IRefreshTokenRepository: JpaRepository<RefreshToken, Long> {
|
||||
@Query("SELECT * FROM refresh_tokens WHERE email = :email", nativeQuery = true)
|
||||
fun findByEmail(@Param("email") email: String): RefreshToken?
|
||||
|
||||
fun getRefreshTokenByEmail(email: String): RefreshToken?
|
||||
|
||||
}
|
||||
|
|
@ -2,11 +2,8 @@ package com.kassaev.notes.repository
|
|||
|
||||
import com.kassaev.notes.model.User
|
||||
import org.springframework.data.jpa.repository.JpaRepository
|
||||
import org.springframework.data.jpa.repository.Query
|
||||
import org.springframework.data.repository.query.Param
|
||||
|
||||
interface IUserRepository: JpaRepository<User, Long> {
|
||||
|
||||
@Query("SELECT * FROM users WHERE email = :email", nativeQuery = true)
|
||||
fun findByEmail(@Param("email") email: String): User?
|
||||
fun getUserByEmail(email: String): User?
|
||||
}
|
||||
|
|
@ -61,7 +61,7 @@ class AuthenticationService(
|
|||
return extractedEmail?.let { email ->
|
||||
val currentUserDetails = userDetailsService.loadUserByUsername(email)
|
||||
|
||||
val refreshTokenInDB = refreshTokenRepository.findByEmail(currentUserDetails.username)
|
||||
val refreshTokenInDB = refreshTokenRepository.getRefreshTokenByEmail(currentUserDetails.username)
|
||||
|
||||
if (!tokenService.isExpired(token) && currentUserDetails.username == refreshTokenInDB?.email)
|
||||
generateAccessToken(currentUserDetails)
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@ class CustomUserDetailsService(
|
|||
private val userRepository: IUserRepository
|
||||
): UserDetailsService {
|
||||
override fun loadUserByUsername(username: String): UserDetails =
|
||||
userRepository.findByEmail(username)
|
||||
userRepository.getUserByEmail(username)
|
||||
?.mapToUserDetails()
|
||||
?: throw UsernameNotFoundException("Not found!")
|
||||
|
||||
|
|
|
|||
|
|
@ -2,25 +2,49 @@ package com.kassaev.notes.service
|
|||
|
||||
import com.kassaev.notes.model.Note
|
||||
import com.kassaev.notes.repository.INoteRepository
|
||||
import org.springframework.http.HttpStatus
|
||||
import org.springframework.http.HttpStatusCode
|
||||
import org.springframework.stereotype.Service
|
||||
import org.springframework.web.server.ResponseStatusException
|
||||
import java.util.*
|
||||
|
||||
@Service
|
||||
class NoteService(val repository: INoteRepository) {
|
||||
|
||||
fun getAllNotes(): List<Note>{
|
||||
return repository.findAll()
|
||||
fun getAllNotes(email: String): List<Note> {
|
||||
return repository.findNotesByAuthor(email)
|
||||
}
|
||||
|
||||
fun getNoteById(id: Long): Optional<Note> {
|
||||
return repository.findById(id)
|
||||
fun getNoteByIdAndAuthor(id: Long, author: String): Note? {
|
||||
return repository.getNoteByIdAndAuthor(
|
||||
id = id,
|
||||
author = author
|
||||
)
|
||||
}
|
||||
|
||||
fun updateNote(note: Note): Note {
|
||||
fun updateNote(noteText: String, author: String, noteId: Long): Int {
|
||||
return repository.updateNote(
|
||||
noteText = noteText,
|
||||
author = author,
|
||||
noteId = noteId
|
||||
)
|
||||
}
|
||||
|
||||
fun createNote(note: Note): Note {
|
||||
return repository.save(note)
|
||||
}
|
||||
|
||||
fun deleteNote(id: Long){
|
||||
fun deleteNote(id: Long, author: String): HttpStatus {
|
||||
val authorInDB = repository.getNoteByIdAndAuthor(
|
||||
id = id,
|
||||
author = author
|
||||
)?.author
|
||||
|
||||
if (authorInDB == author){
|
||||
repository.deleteById(id)
|
||||
return HttpStatus.OK
|
||||
}else{
|
||||
throw ResponseStatusException(HttpStatus.BAD_REQUEST, "Cannot update note.")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -11,7 +11,7 @@ class UserService(
|
|||
private val userRepository: IUserRepository
|
||||
) {
|
||||
fun createUser(user: User): User? {
|
||||
val found = userRepository.findByEmail(user.email)
|
||||
val found = userRepository.getUserByEmail(user.email)
|
||||
|
||||
return if (found == null) {
|
||||
userRepository.save(user)
|
||||
|
|
|
|||
Loading…
Reference in New Issue