note author specified
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run Details

This commit is contained in:
Andrey Kassaev 2024-01-13 14:58:15 +04:00
parent 7d0d60d72f
commit 5f4bc4405d
10 changed files with 91 additions and 38 deletions

View File

@ -1,9 +0,0 @@
on:
pull_request:
branches: [ master ]
push:
branches: [ master ]
jobs:
steps:
run: pwd

View File

@ -3,7 +3,12 @@ package com.kassaev.notes.controller.note
import com.kassaev.notes.model.Note import com.kassaev.notes.model.Note
import com.kassaev.notes.service.NoteService import com.kassaev.notes.service.NoteService
import lombok.AllArgsConstructor 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.bind.annotation.*
import org.springframework.web.server.ResponseStatusException
import java.security.Principal
import java.util.* import java.util.*
@RestController @RestController
@ -12,27 +17,46 @@ import java.util.*
class NoteController(val service: NoteService) { class NoteController(val service: NoteService) {
@GetMapping("/all") @GetMapping("/all")
fun getAllNotes(): List<Note> { fun getAllNotes(principal: Principal): List<Note> {
return service.getAllNotes() // val userEmail = SecurityContextHolder.getContext().authentication.name
return service.getAllNotes(principal.name)
} }
@PostMapping("/create") @PostMapping("/create")
fun createNote(@RequestBody note: Note): Note { fun createNote(@RequestBody note: Note, principal: Principal): Note? {
return service.updateNote(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}") @GetMapping("/{id}")
fun getNoteById(@PathVariable id: Long): Optional<Note> { fun getNoteById(@PathVariable id: Long, principal: Principal): Note? {
return service.getNoteById(id) return service.getNoteByIdAndAuthor(
id = id,
author = principal.name
)
} }
@PutMapping("/update")
fun updateNote(@RequestBody note: Note): Note {
return service.updateNote(note)
}
@DeleteMapping("/remove/{id}") @DeleteMapping("/remove/{id}")
fun deleteNote(@PathVariable id: Long){ fun deleteNote(@PathVariable id: Long, principal: Principal): HttpStatus {
return service.deleteNote(id) return service.deleteNote(
id = id,
author = principal.name
)
} }
} }

View File

@ -9,5 +9,6 @@ data class Note(
@GeneratedValue(strategy = GenerationType.IDENTITY) @GeneratedValue(strategy = GenerationType.IDENTITY)
val id: Long?, val id: Long?,
val text: String, val text: String,
val dateCreated: String val dateCreated: String,
val author: String
) )

View File

@ -1,8 +1,23 @@
package com.kassaev.notes.repository package com.kassaev.notes.repository
import com.kassaev.notes.model.Note import com.kassaev.notes.model.Note
//import jakarta.transaction.Transactional
import org.springframework.data.jpa.repository.JpaRepository 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> { 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?
} }

View File

@ -6,6 +6,7 @@ import org.springframework.data.jpa.repository.Query
import org.springframework.data.repository.query.Param import org.springframework.data.repository.query.Param
interface IRefreshTokenRepository: JpaRepository<RefreshToken, Long> { 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?
} }

View File

@ -2,11 +2,8 @@ package com.kassaev.notes.repository
import com.kassaev.notes.model.User import com.kassaev.notes.model.User
import org.springframework.data.jpa.repository.JpaRepository 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> { interface IUserRepository: JpaRepository<User, Long> {
@Query("SELECT * FROM users WHERE email = :email", nativeQuery = true) fun getUserByEmail(email: String): User?
fun findByEmail(@Param("email") email: String): User?
} }

View File

@ -61,7 +61,7 @@ class AuthenticationService(
return extractedEmail?.let { email -> return extractedEmail?.let { email ->
val currentUserDetails = userDetailsService.loadUserByUsername(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) if (!tokenService.isExpired(token) && currentUserDetails.username == refreshTokenInDB?.email)
generateAccessToken(currentUserDetails) generateAccessToken(currentUserDetails)

View File

@ -13,7 +13,7 @@ class CustomUserDetailsService(
private val userRepository: IUserRepository private val userRepository: IUserRepository
): UserDetailsService { ): UserDetailsService {
override fun loadUserByUsername(username: String): UserDetails = override fun loadUserByUsername(username: String): UserDetails =
userRepository.findByEmail(username) userRepository.getUserByEmail(username)
?.mapToUserDetails() ?.mapToUserDetails()
?: throw UsernameNotFoundException("Not found!") ?: throw UsernameNotFoundException("Not found!")

View File

@ -2,25 +2,49 @@ package com.kassaev.notes.service
import com.kassaev.notes.model.Note import com.kassaev.notes.model.Note
import com.kassaev.notes.repository.INoteRepository import com.kassaev.notes.repository.INoteRepository
import org.springframework.http.HttpStatus
import org.springframework.http.HttpStatusCode
import org.springframework.stereotype.Service import org.springframework.stereotype.Service
import org.springframework.web.server.ResponseStatusException
import java.util.* import java.util.*
@Service @Service
class NoteService(val repository: INoteRepository) { class NoteService(val repository: INoteRepository) {
fun getAllNotes(): List<Note>{ fun getAllNotes(email: String): List<Note> {
return repository.findAll() return repository.findNotesByAuthor(email)
} }
fun getNoteById(id: Long): Optional<Note> { fun getNoteByIdAndAuthor(id: Long, author: String): Note? {
return repository.findById(id) 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) 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) repository.deleteById(id)
return HttpStatus.OK
}else{
throw ResponseStatusException(HttpStatus.BAD_REQUEST, "Cannot update note.")
}
} }
} }

View File

@ -11,7 +11,7 @@ class UserService(
private val userRepository: IUserRepository private val userRepository: IUserRepository
) { ) {
fun createUser(user: User): User? { fun createUser(user: User): User? {
val found = userRepository.findByEmail(user.email) val found = userRepository.getUserByEmail(user.email)
return if (found == null) { return if (found == null) {
userRepository.save(user) userRepository.save(user)