diff --git a/backend/pom.xml b/backend/pom.xml index 322f26b..a350ee1 100644 --- a/backend/pom.xml +++ b/backend/pom.xml @@ -66,6 +66,11 @@ joda-time joda-time + + com.fasterxml.jackson.datatype + jackson-datatype-joda + 2.9.5 + org.springframework.boot diff --git a/backend/src/main/kotlin/uk/co/neviyn/observationdatabase/Controller.kt b/backend/src/main/kotlin/uk/co/neviyn/observationdatabase/Controller.kt index ded4713..dd46e3f 100644 --- a/backend/src/main/kotlin/uk/co/neviyn/observationdatabase/Controller.kt +++ b/backend/src/main/kotlin/uk/co/neviyn/observationdatabase/Controller.kt @@ -27,12 +27,14 @@ class Controller { } @GetMapping("/site/{id}/tutors") - fun getTutorsForSite(@PathVariable(value = "id") id: Long): List = - siteRepository.findById(id).map { site -> - site.tutors.map { NameValue(it.name, it.id) } - }.get() + fun getTutorsForSite(@PathVariable(value = "id") id: Long): List? { + val site = siteRepository.findById(id) + if (site.isPresent) + return site.map { site1 -> site1.tutors.map { NameValue(it.name, it.id) } }.get() + return null + } - @GetMapping + @GetMapping("/tutor") fun getAllTutors(): List = tutorRepository.findAll().map { NameValue(it.name, it.id) } @PostMapping("/tutor") @@ -55,11 +57,12 @@ class Controller { @PostMapping("/observation") fun addObservation(@Valid @RequestBody newObservation: NewObservation): Long { + println(newObservation) val site = siteRepository.findById(newObservation.site).get() val tutors = tutorRepository.findAllById(newObservation.tutors).toSet() val overallScores = newObservation.entries.asSequence().groupBy { it.type } .map { entry -> entry.key to entry.value.asSequence().mapNotNull { it.rating }.average() } - .map { it.first to if(it.second > 0) it.second else null }.toList() + .map { it.first to if (it.second > 0) it.second else null }.toList() .toMap() var observation = Observation( site = site, @@ -80,51 +83,56 @@ class Controller { it.observations.add(observation) tutorRepository.save(it) } + println(observation) return observation.id } - @GetMapping("/observations") - fun getObservations(@Valid @RequestBody observationsRequest: ObservationsRequest): List? { + @PostMapping("/observations") + fun getObservations(@Valid @RequestBody observationsRequest: ObservationsRequest): List { + print(observationsRequest) if (observationsRequest.tutor != null) { - val tutor = tutorRepository.findById(observationsRequest.tutor).get() - return if (observationsRequest.whom == null) { - observationRepository.findByTutorsAndDateBetween(tutor = tutor, - startDate = observationsRequest.startDate, - endDate = observationsRequest.endDate) - } else { - observationRepository.findByTutorsAndWhomAndDateBetween(tutor = tutor, - whom = observationsRequest.whom, - startDate = observationsRequest.startDate, - endDate = observationsRequest.endDate) - } + return tutorRepository.findById(observationsRequest.tutor).map { + when { + (observationsRequest.whom == null || observationsRequest.whom.isEmpty()) -> observationRepository.findByTutorsAndDateBetween(tutor = it, + startDate = observationsRequest.startDate, + endDate = observationsRequest.endDate) + else -> observationRepository.findByTutorsAndWhomAndDateBetween(tutor = it, + whom = observationsRequest.whom, + startDate = observationsRequest.startDate, + endDate = observationsRequest.endDate) + } + }.orElse(listOf()) } if (observationsRequest.site != null) { - val site = siteRepository.findById(observationsRequest.site).get() - return if (observationsRequest.whom == null) { - observationRepository.findBySiteAndDateBetween(site = site, - startDate = observationsRequest.startDate, - endDate = observationsRequest.endDate) - } else { - observationRepository.findBySiteAndWhomAndDateBetween(site = site, - whom = observationsRequest.whom, - startDate = observationsRequest.startDate, - endDate = observationsRequest.endDate) - } + return siteRepository.findById(observationsRequest.site).map { + when { + (observationsRequest.whom == null || observationsRequest.whom.isEmpty()) -> observationRepository.findBySiteAndDateBetween(site = it, + startDate = observationsRequest.startDate, + endDate = observationsRequest.endDate) + else -> observationRepository.findBySiteAndWhomAndDateBetween(site = it, + whom = observationsRequest.whom, + startDate = observationsRequest.startDate, + endDate = observationsRequest.endDate) + } + }.orElse(listOf()) } - return null + return listOf() } - @GetMapping("/observations/chartdata") + @PostMapping("/observations/chartdata") fun getObservationsChartData(@Valid @RequestBody observationsRequest: ObservationsRequest): ChartData? { - val data = getObservations(observationsRequest) ?: return null - val groupedData = data.asSequence().groupBy { it.date }.map{ entry -> AverageData( - entry.value.asSequence().mapNotNull { it.monitoring }.average(), - entry.value.asSequence().mapNotNull { it.control }.average(), - entry.value.asSequence().mapNotNull { it.conservatism }.average(), - entry.value.asSequence().mapNotNull { it.teamwork }.average(), - entry.value.asSequence().mapNotNull { it.knowledge }.average(), - entry.key - )}.toList() + val data = getObservations(observationsRequest) + if(data.isEmpty()) return ChartData(listOf(), listOf()) + val groupedData = data.asSequence().groupBy { it.date }.map { entry -> + AverageData( + entry.value.asSequence().mapNotNull { it.monitoring }.average(), + entry.value.asSequence().mapNotNull { it.control }.average(), + entry.value.asSequence().mapNotNull { it.conservatism }.average(), + entry.value.asSequence().mapNotNull { it.teamwork }.average(), + entry.value.asSequence().mapNotNull { it.knowledge }.average(), + entry.key + ) + }.toList() val dates = groupedData.map { it.date.toString("yyyy-MM-dd") } return ChartData( labels = dates, diff --git a/backend/src/main/kotlin/uk/co/neviyn/observationdatabase/Entity.kt b/backend/src/main/kotlin/uk/co/neviyn/observationdatabase/Entity.kt index 27e7310..6f59dcd 100644 --- a/backend/src/main/kotlin/uk/co/neviyn/observationdatabase/Entity.kt +++ b/backend/src/main/kotlin/uk/co/neviyn/observationdatabase/Entity.kt @@ -1,5 +1,6 @@ package uk.co.neviyn.observationdatabase +import com.fasterxml.jackson.annotation.JsonIgnore import com.fasterxml.jackson.annotation.JsonProperty import org.joda.time.LocalDate import javax.persistence.* @@ -43,6 +44,7 @@ data class Observation( @Id @GeneratedValue(strategy = GenerationType.IDENTITY) val id: Long = 0, + @JsonIgnore @ManyToOne val site: Site, @Column(nullable = false) @@ -60,6 +62,7 @@ data class Observation( val knowledge: Double?, @ElementCollection val entries: List, + @JsonIgnore @ManyToMany(mappedBy = "observations") val tutors: Set ) @@ -72,10 +75,10 @@ data class Entry( @Column(nullable = false) @JsonProperty val rating: Int, - @Column(nullable = false) + @Column(nullable = false, columnDefinition = "TEXT") @JsonProperty val strengths: String, - @Column(nullable = false) + @Column(nullable = false, columnDefinition = "TEXT") @JsonProperty val improvements: String ) \ No newline at end of file diff --git a/backend/src/main/resources/application.properties b/backend/src/main/resources/application.properties index dcc84e6..5917ade 100644 --- a/backend/src/main/resources/application.properties +++ b/backend/src/main/resources/application.properties @@ -1 +1,3 @@ -spring.jpa.properties.jadira.usertype.autoRegisterUserTypes = true \ No newline at end of file +spring.jpa.properties.jadira.usertype.autoRegisterUserTypes = true +spring.datasource.url=jdbc:h2:file:./database +spring.jpa.hibernate.ddl-auto=update diff --git a/frontend/src/App.vue b/frontend/src/App.vue index 3411d5a..adfbb5e 100644 --- a/frontend/src/App.vue +++ b/frontend/src/App.vue @@ -1,37 +1,42 @@ diff --git a/frontend/src/views/Home.vue b/frontend/src/views/Home.vue index 113e542..0808487 100644 --- a/frontend/src/views/Home.vue +++ b/frontend/src/views/Home.vue @@ -1,145 +1,154 @@ diff --git a/frontend/src/views/Stats.vue b/frontend/src/views/Stats.vue index 3c5318e..2b8bfb5 100644 --- a/frontend/src/views/Stats.vue +++ b/frontend/src/views/Stats.vue @@ -14,12 +14,17 @@ - + + + + + + - + @@ -103,12 +108,12 @@ export default { methods: { getFilteredAverage: function() { Vue.axios - .get("/observations/chartdata", { - site: this.siteSelection, - tutor: this.tutorSelection, - startDate: moment(this.startDate).format("YYYY-MM-DD"), - endDate: moment(this.endDate).format("YYYY-MM-DD"), - whom: this.whom + .post("/observations/chartdata", { + 'site': this.siteSelection, + 'tutor': this.tutorSelection, + 'startDate': moment(this.startDate).format("YYYY-MM-DD"), + 'endDate': moment(this.endDate).format("YYYY-MM-DD"), + 'whom': this.whom }) .then(response => { this.chartData = response.data; @@ -137,7 +142,7 @@ export default { }, watch: { siteSelection: function() { - this.tutorOptions = null; + this.tutorOptions = []; this.tutorSelection = null; this.getTutors(); } @@ -145,7 +150,7 @@ export default { mounted() { this.getFilteredAverage(); Vue.axios - .get("/site/all") + .get("/site") .then(response => { this.siteOptions = response.data; }) diff --git a/frontend/src/views/ViewObservations.vue b/frontend/src/views/ViewObservations.vue index 2763222..5dd159e 100644 --- a/frontend/src/views/ViewObservations.vue +++ b/frontend/src/views/ViewObservations.vue @@ -1,114 +1,150 @@