Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
Serge-NSN committed Jul 19, 2023
1 parent 301f39f commit a818486
Show file tree
Hide file tree
Showing 36 changed files with 2,146 additions and 155 deletions.
Binary file modified data/QAData.mv.db
Binary file not shown.
1,972 changes: 1,972 additions & 0 deletions data/QAData.trace.db

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package com.serge.nsn.qanda_hub
package com.serge.nsn.qahub

import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication

@SpringBootApplication

class QandAHubApplication
class QAHubApplication

fun main(args: Array<String>) {
runApplication<QandAHubApplication>(*args)
runApplication<QAHubApplication>(*args)
}

//typealias Id = UUID
//val id = Id()
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
package com.serge.nsn.qanda_hub.api.controller
package com.serge.nsn.qahub.api.controller

import com.serge.nsn.qanda_hub.api.dto.AnswerDto
import com.serge.nsn.qanda_hub.services.AnswerService
import com.serge.nsn.qahub.api.dto.AnswerDto
import com.serge.nsn.qahub.services.AnswerService
import lombok.RequiredArgsConstructor
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
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
import org.springframework.web.bind.annotation.*

@RestController
@RequiredArgsConstructor
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package com.serge.nsn.qanda_hub.api.controller
package com.serge.nsn.qahub.api.controller

import com.serge.nsn.qanda_hub.api.dto.UserDto
import com.serge.nsn.qanda_hub.configuration.jwt.JWTService
import com.serge.nsn.qanda_hub.configuration.jwt.LoginRequest
import com.serge.nsn.qanda_hub.services.UserService
import com.serge.nsn.qahub.api.dto.UserDto
import com.serge.nsn.qahub.api.model.LoginRequest
import com.serge.nsn.qahub.api.model.LoginResponse
import com.serge.nsn.qahub.configuration.jwt.JWTService
import com.serge.nsn.qahub.services.UserService
import lombok.RequiredArgsConstructor
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
Expand All @@ -29,13 +30,22 @@ class AuthenticationController(
ResponseEntity(userService.register_user(dto), HttpStatus.CREATED)

@PostMapping("/login")
fun login(@RequestBody request: LoginRequest): String {
fun login(@RequestBody request: LoginRequest): LoginResponse {
val authentication = authenticationManager
.authenticate(UsernamePasswordAuthenticationToken(request.userName, request.password))
.authenticate(UsernamePasswordAuthenticationToken(request.username, request.password))
return if (authentication.isAuthenticated) {
jwtService.generateToken(request.userName)
LoginResponse(accessToken = jwtService.generateToken(request.username))
} else {
throw UsernameNotFoundException("Invalid user credentials")
}
}

@RequestMapping("/batch-registration")


@PostMapping
fun createUsers(@RequestBody newUsers: List<UserDto>): ResponseEntity<List<UserDto>> {
return ResponseEntity(userService.batchRegister(newUsers), HttpStatus.CREATED)

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.serge.nsn.qahub.api.controller

import com.serge.nsn.qahub.api.dto.CommentDto
import com.serge.nsn.qahub.services.CommentService
import org.springframework.web.bind.annotation.*

@RestController
@RequestMapping("/api/comment")
class CommentController(
private val commentService: CommentService
) {

@GetMapping("/all")
fun allComments() = commentService.getAllComments()

@GetMapping("/{id}")
fun getComment(@PathVariable id: Long) = commentService.getComment(id)

@PostMapping("/reply")
fun reply(@RequestBody comment: CommentDto) = commentService.comment(comment)

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.serge.nsn.qanda_hub.api.controller
package com.serge.nsn.qahub.api.controller

import com.serge.nsn.qanda_hub.api.dto.UserDto
import com.serge.nsn.qanda_hub.data.entities.UserEntity
import com.serge.nsn.qanda_hub.services.UserService
import com.serge.nsn.qahub.api.dto.UserDto
import com.serge.nsn.qahub.services.UserService
import org.springframework.web.bind.annotation.*
import java.security.Principal

@RestController
@CrossOrigin
Expand All @@ -18,4 +18,10 @@ class UserController(
@GetMapping("/{userId}")
fun getUser(@PathVariable userId: Long) = userService.getById(userId)

@GetMapping("/user")
fun getCurrentUser(principal: Principal): String {
val username = principal.name
return "Current user: $username"
}

}
13 changes: 13 additions & 0 deletions src/main/kotlin/com/serge/nsn/qahub/api/dto/AnswerDto.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.serge.nsn.qahub.api.dto

import com.serge.nsn.qahub.data.entities.AnswerEntity

class AnswerDto(
val id: Long? = null,
val content: String = "",
) {
constructor(answerEntity: AnswerEntity) : this(
id = answerEntity.id,
content = answerEntity.content
)
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.serge.nsn.qanda_hub.api.dto
package com.serge.nsn.qahub.api.dto

class CommentDto(
val comment_id: Long? = null,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
package com.serge.nsn.qanda_hub.api.dto
package com.serge.nsn.qahub.api.dto

import com.serge.nsn.qanda_hub.data.entities.QuestionEntity
import com.serge.nsn.qahub.data.entities.QuestionEntity

class QuestionDto(
val question_id: Long? = null,
val id: Long? = null,
val title: String = "",
val content: String = "",
val author: String = "",
// val user: UserDto? = null
) {
constructor(questionEntity: QuestionEntity) : this(
question_id = questionEntity.id,
id = questionEntity.id,
title = questionEntity.title,
content = questionEntity.content,
// user = UserDto(questionEntity.user)
author = questionEntity.author,
// user = questionEntity.user
)
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.serge.nsn.qanda_hub.api.dto
package com.serge.nsn.qahub.api.dto

import com.serge.nsn.qanda_hub.data.entities.UserEntity
import com.serge.nsn.qahub.data.entities.UserEntity

class UserDto(
val user_id: Long? = null,
Expand All @@ -9,14 +9,14 @@ class UserDto(
val password: String = "",
val email: String = "",
val role: String = "",
val questions: List<QuestionDto> = listOf()
) {

) {
constructor(userEntity: UserEntity) : this(
user_id = userEntity.id,
name = userEntity.name,
username = userEntity.username,
email = userEntity.email,
role = userEntity.roles,
questions = userEntity.questions.map { QuestionDto(it) }
// questions = userEntity.questions.map { QuestionDto(it) }
)
}
6 changes: 6 additions & 0 deletions src/main/kotlin/com/serge/nsn/qahub/api/model/LoginRequest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.serge.nsn.qahub.api.model

class LoginRequest(
val username: String,
val password: String
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.serge.nsn.qahub.api.model

data class LoginResponse(val accessToken: String)
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.serge.nsn.qanda_hub.configuration
package com.serge.nsn.qahub.configuration

import com.serge.nsn.qanda_hub.data.entities.UserEntity
import com.serge.nsn.qahub.data.entities.UserEntity
import lombok.Data
import org.springframework.security.core.GrantedAuthority
import org.springframework.security.core.authority.SimpleGrantedAuthority
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.serge.nsn.qanda_hub.configuration
package com.serge.nsn.qahub.configuration

import com.serge.nsn.qanda_hub.data.repositories.UserRepository
import com.serge.nsn.qahub.data.repositories.UserRepository
import lombok.RequiredArgsConstructor
import org.springframework.security.core.userdetails.UserDetails
import org.springframework.security.core.userdetails.UserDetailsService
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.serge.nsn.qanda_hub.configuration
package com.serge.nsn.qahub.configuration

import com.serge.nsn.qanda_hub.configuration.jwt.JWTAuthenticationFilter
import com.serge.nsn.qanda_hub.configuration.jwt.JwtAuthenticationEntryPoint
import com.serge.nsn.qahub.configuration.jwt.JWTAuthenticationFilter
import com.serge.nsn.qahub.configuration.jwt.JwtAuthenticationEntryPoint
import lombok.RequiredArgsConstructor
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
Expand All @@ -18,6 +18,9 @@ import org.springframework.security.crypto.password.PasswordEncoder
import org.springframework.security.web.SecurityFilterChain
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter
import org.springframework.security.web.authentication.www.BasicAuthenticationFilter
import org.springframework.web.cors.CorsConfiguration
import org.springframework.web.cors.UrlBasedCorsConfigurationSource
import org.springframework.web.filter.CorsFilter

@Configuration
@EnableWebSecurity
Expand All @@ -29,7 +32,7 @@ class SecurityConfig(
) {

companion object {
val UN_SECURED_URL = arrayOf("/api/auth/**", "/")
val UN_SECURED_URL = arrayOf("/api/auth/**", "/api/auth/login")
}

@Bean
Expand Down Expand Up @@ -71,4 +74,18 @@ class SecurityConfig(
fun authenticationManager(authConfig: AuthenticationConfiguration): AuthenticationManager {
return authConfig.authenticationManager
}

@Bean
fun corsConfig(): CorsFilter {
val config = CorsConfiguration()
config.addAllowedOrigin("http://192.168.119.99:4200")
config.addAllowedOrigin("http://localhost:4200")
config.addAllowedMethod("POST")
config.addAllowedMethod("GET")
config.addAllowedHeader("*")
val source = UrlBasedCorsConfigurationSource()
source.registerCorsConfiguration("/**", config)
return CorsFilter(source)
}

}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package com.serge.nsn.qanda_hub.configuration.exceptions
package com.serge.nsn.qahub.configuration.exceptions

class UserNotFoundException(message: String?) : RuntimeException(message)
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.serge.nsn.qanda_hub.configuration.jwt
package com.serge.nsn.qahub.configuration.jwt

import com.serge.nsn.qanda_hub.configuration.QAUserDetailsService
import com.serge.nsn.qahub.configuration.QAUserDetailsService
import jakarta.servlet.FilterChain
import jakarta.servlet.ServletException
import jakarta.servlet.http.HttpServletRequest
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.serge.nsn.qanda_hub.configuration.jwt
package com.serge.nsn.qahub.configuration.jwt

import io.jsonwebtoken.Claims
import io.jsonwebtoken.Jwts
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.serge.nsn.qanda_hub.configuration.jwt
package com.serge.nsn.qahub.configuration.jwt

import com.fasterxml.jackson.databind.ObjectMapper
import jakarta.servlet.http.HttpServletRequest
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.serge.nsn.qanda_hub.data.entities
package com.serge.nsn.qahub.data.entities

import jakarta.persistence.*

@Entity
data class AnswerEntity(
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
val answer_id: Long? = null,
val id: Long? = null,
val content: String,

@ManyToOne(fetch = FetchType.EAGER)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.serge.nsn.qanda_hub.data.entities
package com.serge.nsn.qahub.data.entities

import jakarta.persistence.*

@Entity
data class CommentEntity(
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
val comment_id: Long? = null,
val id: Long? = null,
val content: String,

@ManyToOne(fetch = FetchType.EAGER)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.serge.nsn.qanda_hub.data.entities
package com.serge.nsn.qahub.data.entities

import jakarta.persistence.*

Expand All @@ -9,6 +9,7 @@ data class QuestionEntity(
val id: Long? = null,
val title: String = "",
val content: String = "",
val author: String = "",

@ManyToOne
@JoinColumn(name = "user_id")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.serge.nsn.qanda_hub.data.entities
package com.serge.nsn.qahub.data.entities

import jakarta.persistence.*

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.serge.nsn.qanda_hub.data.repositories
package com.serge.nsn.qahub.data.repositories

import com.serge.nsn.qanda_hub.data.entities.AnswerEntity
import com.serge.nsn.qahub.data.entities.AnswerEntity
import org.springframework.data.jpa.repository.JpaRepository

interface AnswerRepository : JpaRepository<AnswerEntity, Long>
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.serge.nsn.qanda_hub.data.repositories
package com.serge.nsn.qahub.data.repositories

import com.serge.nsn.qanda_hub.data.entities.CommentEntity
import com.serge.nsn.qahub.data.entities.CommentEntity
import org.springframework.data.jpa.repository.JpaRepository

interface CommentRepository : JpaRepository<CommentEntity, Long>
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.serge.nsn.qanda_hub.data.repositories
package com.serge.nsn.qahub.data.repositories

import com.serge.nsn.qanda_hub.data.entities.QuestionEntity
import com.serge.nsn.qahub.data.entities.QuestionEntity
import org.springframework.data.jpa.repository.JpaRepository

interface QuestionRepository : JpaRepository<QuestionEntity, Long> {
Expand Down
Loading

0 comments on commit a818486

Please sign in to comment.