-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Modified QuestionEntity to have author property gotten from Principal
- Loading branch information
Showing
3 changed files
with
69 additions
and
33 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
src/main/kotlin/com/serge/nsn/qahub/api/controller/QuestionController.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.serge.nsn.qahub.api.controller | ||
|
||
import com.serge.nsn.qahub.api.dto.QuestionDto | ||
import com.serge.nsn.qahub.services.QuestionService | ||
import org.springframework.web.bind.annotation.* | ||
import java.security.Principal | ||
|
||
@RestController | ||
@RequestMapping("/api/question") | ||
class QuestionController( | ||
private val questionService: QuestionService | ||
) { | ||
|
||
@GetMapping("/all") | ||
fun getAll() = questionService.getAllQuestions() | ||
|
||
@GetMapping("/{id}") | ||
fun get(@PathVariable id: Long) = questionService.getQuestionById(id) | ||
|
||
@GetMapping("/user/{userId}") | ||
fun getByUserId(@PathVariable userId: Long): List<QuestionDto> { | ||
return questionService.getByUserId(userId) | ||
} | ||
|
||
@PostMapping("/ask") | ||
fun ask(@RequestBody question: QuestionDto, principal: Principal) = questionService.askQuestion(question, principal) | ||
} |
42 changes: 42 additions & 0 deletions
42
src/main/kotlin/com/serge/nsn/qahub/services/QuestionService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.serge.nsn.qahub.services | ||
|
||
import com.serge.nsn.qahub.api.dto.QuestionDto | ||
import com.serge.nsn.qahub.data.entities.QuestionEntity | ||
import com.serge.nsn.qahub.data.entities.UserEntity | ||
import com.serge.nsn.qahub.data.repositories.QuestionRepository | ||
import com.serge.nsn.qahub.data.repositories.UserRepository | ||
import org.springframework.http.HttpStatus | ||
import org.springframework.http.ResponseEntity | ||
import org.springframework.stereotype.Service | ||
import java.security.Principal | ||
|
||
@Service | ||
class QuestionService( | ||
private val questionRepository: QuestionRepository, | ||
private val userRepository: UserRepository | ||
) { | ||
fun askQuestion(dto: QuestionDto, principal: Principal): ResponseEntity<QuestionDto> { | ||
val questionAuthor: UserEntity = userRepository.findByEmail(principal.name).get() | ||
val askedQuestion = questionRepository | ||
.save( | ||
QuestionEntity( | ||
id = dto.id, | ||
title = dto.title, | ||
content = dto.content, | ||
author = questionAuthor.name, | ||
user = questionAuthor | ||
) | ||
|
||
) | ||
|
||
return ResponseEntity(QuestionDto(askedQuestion), HttpStatus.CREATED) | ||
} | ||
|
||
fun getAllQuestions() = questionRepository.findAll().map { QuestionDto(it) } | ||
fun getQuestionById(id: Long) = questionRepository.findById(id).map { QuestionDto(it) } | ||
|
||
fun getByUserId(userId: Long): List<QuestionDto> { | ||
return questionRepository.findAllByUserId(userId).map { QuestionDto(it) } | ||
} | ||
|
||
} |
33 changes: 0 additions & 33 deletions
33
src/main/kotlin/com/serge/nsn/qanda_hub/services/QuestionService.kt
This file was deleted.
Oops, something went wrong.