Esempio n. 1
0
        public Response AddPlayerToAssignment(PlayerAssignmentRequest request)
        {
            var club = clubQuery.GetClub(request.ClubId);

            if (club == null)
            {
                return(Response.CreateResponse(new EntityNotFoundException("The specified club does not exist")));
            }
            else if (request.PlayerId.IsEmpty())
            {
                return(Response.CreateResponse(new IllegalOperationException("PlayerId is required")));
            }
            else if (request.CoachId.IsEmpty())
            {
                return(Response.CreateResponse(new IllegalOperationException("CoachId is required")));
            }

            var playerAssignment = assignmentQuery.GetPlayerAssignment(new Core.Queries.PlayerAssignmentQuery {
                ClubId = request.ClubId, AssignmentId = request.AssignmentId, PlayerId = request.PlayerId
            });

            if (playerAssignment != null)
            {
                return(Response.CreateResponse(new IllegalOperationException("This assignment has already been allocated to the specified player")));
            }

            var assignment = assignmentQuery.GetAssignment(request.ClubId, request.AssignmentId);

            if (assignment.ClubId != request.ClubId)
            {
                return(Response.CreateResponse(new IllegalOperationException("The specified assignment does not belong to this club")));
            }

            var coach = memberQuery.GetCoach(request.CoachId.Value);

            if (coach == null)
            {
                return(Response.CreateResponse(new EntityNotFoundException("The specified coach does not exist")));
            }
            else if (coach.ClubId != club.Guid)
            {
                return(Response.CreateResponse(new IllegalOperationException("The specified coach does not belong to this club")));
            }

            try {
                assignmentRepository.AddPlayerToAssignment(request);
                return(Response.CreateSuccessResponse());
            } catch (Exception ex) {
                return(Response.CreateResponse(ex));
            }
        }