Skip to content

Commit

Permalink
Add user endpoint with committess at /api/committee/user
Browse files Browse the repository at this point in the history
  • Loading branch information
akselsf committed Nov 17, 2024
1 parent 2ce322a commit c55a28e
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.example.autobank.controller
import com.example.autobank.data.Committee
import com.example.autobank.data.Economicrequest
import com.example.autobank.data.user.OnlineUser
import com.example.autobank.data.user.UserCommitteeResponseBody
import com.example.autobank.repository.CommitteeRepository
import com.example.autobank.service.AdminService
import com.example.autobank.service.CommitteeService
Expand All @@ -28,6 +29,15 @@ class CommitteeController(
} catch (e: Exception) {
ResponseEntity.badRequest().build()
}
}

@GetMapping("/user")
fun getUserAndCommittees(): ResponseEntity<UserCommitteeResponseBody> {
return try {
val userandcommittees = committeeService.getUserAndCommittees()
ResponseEntity.ok(userandcommittees)
} catch (e: Exception) {
ResponseEntity.badRequest().build()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.example.autobank.data.user;

data class UserCommitteeResponseBody (
val name: String,
val email: String,
val committees: List<String>
)
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,18 @@ class AuthenticationService {
return response.body?.get("id").toString().toInt()
}

private fun fetchUserCommittees(userid: Int): List<String> {
fun fetchUserCommittees(): List<String> {

if (environment != "prod") {
return listOf("Applikasjonskomiteen", "Trivselskomiteen")
}

val userId = fetchOnlineuserId()

val headers = HttpHeaders()
val entity = HttpEntity<Void>(headers)
val response: ResponseEntity<UserCommitteeResponse> = restTemplate.exchange(
fetchUserCommitteesUrl + userid,
fetchUserCommitteesUrl + userId,
HttpMethod.GET,
entity,
object : ParameterizedTypeReference<UserCommitteeResponse>() {}
Expand All @@ -120,7 +126,7 @@ class AuthenticationService {
}

val userId = fetchOnlineuserId()
val userCommittees = fetchUserCommittees(userId)
val userCommittees = fetchUserCommittees()
return userCommittees.contains(adminCommitteeNameLong)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.example.autobank.service;

import com.example.autobank.data.Committee
import com.example.autobank.data.user.UserCommitteeResponseBody
import com.example.autobank.repository.CommitteeRepository
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
Expand All @@ -11,7 +12,15 @@ class CommitteeService() {
@Autowired
lateinit var committeeRepository: CommitteeRepository

@Autowired
lateinit var authenticationService: AuthenticationService

fun getAllCommittees(): List<Committee> {
return committeeRepository.findAll()
}

fun getUserAndCommittees(): UserCommitteeResponseBody {
val userdetails = authenticationService.getUserDetails()
return UserCommitteeResponseBody(userdetails.name, userdetails.email, authenticationService.fetchUserCommittees())
}
}

0 comments on commit c55a28e

Please sign in to comment.