Skip to content

Commit

Permalink
Implement group manager
Browse files Browse the repository at this point in the history
In addition, delete the old clazz manager.
  • Loading branch information
BilledTrain380 committed Aug 19, 2018
1 parent e50c526 commit 9533843
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 211 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,48 +34,35 @@
*
*/

package ch.schulealtendorf.sporttagpsa.business.clazz
package ch.schulealtendorf.sporttagpsa.business.group

import ch.schulealtendorf.sporttagpsa.model.Group
import java.util.*

/**
* Manager for a class or category where a participant belongs to.
* Describes a manager for domain classes related to a {@link Group}.
*
* @author nmaerchy <billedtrain380@gmail.com>
* @since 2.0.0
*/
interface ClassManager {
interface GroupManager {

/**
* @return all classes which are available
* @return true if the given {@code group} has participant, which are not participate in any sport, otherwise false
*/
fun getAllClasses(): List<Group>
fun hasPendingParticipation(group: Group): Boolean

/**
* An {@link Optional} containing the class matching
* the {name} or an empty Optional if no class could be found.
*
* @param name the name of the class
*
* @return the resulting class in an Optional
* @return all groups
*/
fun getClass(name: String): Optional<Group>
fun getGroups(): List<Group>

/**
* Saves the given {@code group}.
*
* If the name of the class exists already, it will be updated, otherwise created.
* Gets the group matching the given {@code name}.
*
* @param clazz the class to save
*/
fun saveClass(clazz: Group)

/**
* A pending participation means, if any participant of the given {@code group}
* as no sport.
* @param name tha name of the group
*
* @return true if the class has pending participation, otherwise false
* @return an Optional containing the group, or empty if the group could not be found
*/
fun hasPendingParticipation(clazz: Group): Boolean
}
fun getGroup(name: String): Optional<Group>
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,73 +34,57 @@
*
*/

package ch.schulealtendorf.sporttagpsa.business.clazz
package ch.schulealtendorf.sporttagpsa.business.group

import ch.schulealtendorf.sporttagpsa.entity.GroupEntity
import ch.schulealtendorf.sporttagpsa.model.Group
import ch.schulealtendorf.sporttagpsa.model.Coach
import ch.schulealtendorf.sporttagpsa.model.Group
import ch.schulealtendorf.sporttagpsa.repository.GroupRepository
import ch.schulealtendorf.sporttagpsa.repository.ParticipantRepository
import org.springframework.stereotype.Component
import java.util.*

/**
* Default class manager implementation which uses the repository classes to get its data.
* A {@link GroupManager} which uses repositories to process its data.
*
* @author nmaerchy <billedtrain380@gmail.com>
* @since 2.0.0
*/
@Component
class DefaultClassManager(
private val classRepository: GroupRepository,
private val competitorRepository: ParticipantRepository
): ClassManager {

/**
* @return all classes which are available
*/
override fun getAllClasses(): List<Group> {
return classRepository.findAll()
.mapNotNull { it?.map() }
}
class GroupManagerImpl(
private val groupRepository: GroupRepository,
private val participantRepository: ParticipantRepository
): GroupManager {

/**
* An {@link Optional} containing the class matching
* the {name} or an empty Optional if no class could be found.
*
* @param name the name of the class
*
* @return the resulting class in an Optional
* @return true if the given {@code group} has participant, which are not participate in any sport, otherwise false
*/
override fun getClass(name: String): Optional<Group> {
override fun hasPendingParticipation(group: Group): Boolean {

val clazz = classRepository.findByName(name)
val participants = participantRepository.findByGroupName(group.name)

return clazz.map { it.map() }
return participants.any { it.sport == null }
}

/**
* Saves the given {@code group}.
*
* If the name of the class exists already, it will be updated, otherwise created.
*
* @param clazz the class to save
* @return all groups
*/
override fun saveClass(clazz: Group) {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
override fun getGroups(): List<Group> {
return groupRepository.findAll()
.map { it.toModel() }
}

/**
* A pending participation means, if any participant of the given {@code group}
* as no sport.
* Gets the group matching the given {@code name}.
*
* @param name tha name of the group
*
* @return true if the class has pending participation, otherwise false
* @return an Optional containing the group, or empty if the group could not be found
*/
override fun hasPendingParticipation(clazz: Group) = competitorRepository.findByGroupName(clazz.name).any { it.sport == null }
override fun getGroup(name: String): Optional<Group> {

private fun GroupEntity.map(): Group {
return Group(
name,
Coach(coach.id!!, coach.name))
return groupRepository.findById(name).map { it.toModel() }
}

private fun GroupEntity.toModel() = Group(name, Coach(coach.id!!, coach.name))
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,47 +34,45 @@
*
*/

package ch.schulealtendorf.sporttagpsa.controller.rest.clazz
package ch.schulealtendorf.sporttagpsa.controller.rest.group

import ch.schulealtendorf.sporttagpsa.business.clazz.ClassManager
import ch.schulealtendorf.sporttagpsa.controller.rest.BadRequestException
import ch.schulealtendorf.sporttagpsa.controller.rest.RestClass
import ch.schulealtendorf.sporttagpsa.business.group.GroupManager
import ch.schulealtendorf.sporttagpsa.controller.rest.NotFoundException
import ch.schulealtendorf.sporttagpsa.controller.rest.RestGroup
import ch.schulealtendorf.sporttagpsa.model.Group
import org.springframework.http.MediaType
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RestController

/**
* Rest Controller for class or category of a participant.
* Rest controller for {@link Group}.
*
* @author nmaerchy <billedtrain380@gmail.com>
* @since 2.0.0
*/
@RestController
class ClassController(
private val classManager: ClassManager
class GroupController(
private val groupManager: GroupManager
) {

@GetMapping("/classes", produces = [MediaType.APPLICATION_JSON_VALUE])
fun getAllClasses(): List<RestClass> {
return classManager.getAllClasses()
.map { it.map() }
@GetMapping("/group/{group_name}", produces = [MediaType.APPLICATION_JSON_VALUE])
fun getGroup(@PathVariable("group_name") name: String): RestGroup {
return groupManager.getGroup(name)
.map { it.toRest() }
.orElseThrow { NotFoundException("Could not find group: name=$name") }
}

@GetMapping("/class/{class_id}", produces = [MediaType.APPLICATION_JSON_VALUE])
fun getClass(@PathVariable("class_id") classId: String): RestClass {

val clazz = classManager.getClass(classId).orElseThrow { BadRequestException("Could not find class with id '$classId'") }

return clazz.map()
@GetMapping("/groups", produces = [MediaType.APPLICATION_JSON_VALUE])
fun getGroups(): List<RestGroup> {
return groupManager.getGroups().map { it.toRest() }
}

private fun Group.map(): RestClass {
return RestClass(
private fun Group.toRest(): RestGroup {
return RestGroup(
name,
coach.name,
classManager.hasPendingParticipation(this)
groupManager.hasPendingParticipation(this)
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@

package ch.schulealtendorf.sporttagpsa.controller.rest.participant

import ch.schulealtendorf.sporttagpsa.business.group.GroupManager
import ch.schulealtendorf.sporttagpsa.business.participation.ParticipantManager
import ch.schulealtendorf.sporttagpsa.business.participation.ParticipationManager
import ch.schulealtendorf.sporttagpsa.controller.rest.*
import ch.schulealtendorf.sporttagpsa.model.*
import org.springframework.http.MediaType
import org.springframework.web.bind.annotation.*
import kotlin.math.abs

/**
* Rest controller for the participants.
Expand All @@ -53,7 +53,8 @@ import kotlin.math.abs
@RestController
class ParticipantController(
private val participantManager: ParticipantManager,
private val participationManager: ParticipationManager
private val participationManager: ParticipationManager,
private val groupManager: GroupManager
) {

companion object {
Expand Down Expand Up @@ -158,7 +159,7 @@ class ParticipantController(
return RestGroup(
name,
coach.name,
true // TODO: use service to get pending status
groupManager.hasPendingParticipation(this)
)
}

Expand Down

This file was deleted.

0 comments on commit 9533843

Please sign in to comment.