Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
zwolsman committed Jun 16, 2018
2 parents c1574ee + 99d8cdd commit 231cfac
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 5 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ dependencies {
compile('org.springframework.boot:spring-boot-starter-data-jpa')
compile('org.springframework.boot:spring-boot-starter-web')
compile('org.springframework.boot:spring-boot-starter-security')
compile('org.springframework.boot:spring-boot-starter-amqp')
compile('com.fasterxml.jackson.module:jackson-module-kotlin')
compile('org.springframework.cloud:spring-cloud-starter-openfeign')
compile("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ interface VehicleClient {

@GetMapping("{license}")
fun getById(@PathVariable license: String, @RequestHeader(HttpHeaders.AUTHORIZATION) auth: String) : SimpleVehicle?

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class SecurityConfig(private val userDetailsService: UserDetailsService) : WebSe
.authorizeRequests()
.antMatchers("/v2/api-docs","/swagger-resources","/swagger-resources/**", "/swagger-ui.html", "/webjars/**").permitAll()
.antMatchers("/actuator/**").permitAll()
.antMatchers(HttpMethod.POST,"/api/trips").permitAll()
.antMatchers(HttpMethod.POST,"/api/trips", "/api/trips/**/finished").permitAll()
.anyRequest().authenticated()
.and()
.addFilterAfter(JwtAuthorizationFilter(authenticationManager(), userDetailsService), BasicAuthenticationFilter::class.java)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,8 @@ class TripController(private val tripService: TripService) {
@GetMapping
fun getTripsForVehicle(@RequestParam vehicleId: String, principal: Principal) = tripService.byVehicleId(vehicleId)

@PostMapping
@RequestMapping("{id}/finished")
fun finishTrip(@PathVariable id: Long) = tripService.finishTrip(id)

}
13 changes: 13 additions & 0 deletions src/main/kotlin/com/s63d/ertripservice/domain/intern/InternTrip.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.s63d.ertripservice.domain.intern

import com.s63d.ertripservice.domain.db.Trip
import com.s63d.ertripservice.domain.db.toPolyline
import com.s63d.ertripservice.domain.rest.SimpleVehicle

data class InternTrip(val id: Long, val polyline: String, val vehicleWeight: Int) {

private constructor() : this(0, "", 0)

constructor(trip:Trip, vehicle: SimpleVehicle) : this(trip.id, trip.locations.toPolyline(), vehicle.weight)

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ package com.s63d.ertripservice.domain.rest
import com.fasterxml.jackson.annotation.JsonIgnoreProperties

@JsonIgnoreProperties(ignoreUnknown = true)
class SimpleVehicle(val carTrackerId: String? = null)
class SimpleVehicle(val carTrackerId: String? = null, val weight: Int)
13 changes: 10 additions & 3 deletions src/main/kotlin/com/s63d/ertripservice/services/TripService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,35 @@ package com.s63d.ertripservice.services
import com.s63d.ertripservice.clients.VehicleClient
import com.s63d.ertripservice.domain.db.CarTracker
import com.s63d.ertripservice.domain.db.Trip
import com.s63d.ertripservice.domain.intern.InternTrip
import com.s63d.ertripservice.domain.rest.TripResponse
import com.s63d.ertripservice.repositories.TripRepository
import org.springframework.amqp.rabbit.core.RabbitTemplate
import org.springframework.data.domain.Page
import org.springframework.data.domain.Pageable
import org.springframework.stereotype.Service

@Service
class TripService(private val tripRepository: TripRepository, private val vehicleClient: VehicleClient) {
class TripService(private val tripRepository: TripRepository, private val vehicleClient: VehicleClient, private val rabbitTemplate: RabbitTemplate) {

fun createNew(carTrackerId: String) : Trip {
val trip = Trip(CarTracker(carTrackerId))
//println(trip.carTracker.carTrackerId)
return tripRepository.save(trip).also(::println)
}

fun byCarTracker(trackerId: String?, pageable: Pageable = Pageable.unpaged()) = tripRepository.findByCarTrackerId(trackerId, pageable).map { TripResponse(it) }

// fun byId(id: Long) = TripResponse(tripRepository.findById(id).orElseThrow { Exception("Invalid trip id") }) // TODO response status

val user1 = " eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJnb29zamVAaG90bWFpbC5jb20iLCJ1c2VyUm9sZSI6ImJhc2ljIiwidXNlcklkIjoxfQ.QPLlsebqlIdn6FQIMThrIQcNtk7qygStwyUcaxETXHQ"
fun byVehicleId(id: String, pageable: Pageable = Pageable.unpaged()): Page<TripResponse> {
val carTrackerId = vehicleClient.getById(id, user1)?.carTrackerId
return byCarTracker(carTrackerId, pageable)
}

fun finishTrip(id: Long) {
val trip = tripRepository.findById(id).orElseThrow { Exception("Trip '$id' not found") }
val vehicle = vehicleClient.getById(trip.carTracker.carTrackerId, user1) ?: throw Exception("Car not found with car tracker ${trip.carTracker.carTrackerId}")
val internTrip = InternTrip(trip, vehicle)
rabbitTemplate.convertAndSend("AT", "trip", internTrip)
}
}
3 changes: 3 additions & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

spring:
datasource:
url: jdbc:mysql://localhost:3309/trip-db
Expand All @@ -10,6 +11,8 @@ spring:
show-sql: true
main:
banner-mode: "off"
rabbitmq:
host: ersols.online
urls:
account-service: "localhost:8081"
vehicle-service: "localhost:8080"
Expand Down

0 comments on commit 231cfac

Please sign in to comment.