Exemple #1
0
        public HttpResponseMessage AddBoat([FromBody] Boat boat)
        {
            //Gets the user from token
            var            token  = Request.Headers.Authorization.Parameter;
            User           sender = UserUtility.GetUserByToken(token);
            CustomResponse response;

            //Controls whether the user is leader or not
            if (sender.team == null || sender.team.leaderId != sender.id)
            {
                response = ResponseMessageHelper.CreateResponse(HttpStatusCode.Unauthorized, true, HttpStatusCode.Unauthorized, ConstantResponse.TEAM_BOAT_FAILED);
                return(Request.CreateResponse(HttpStatusCode.Unauthorized, response));
            }
            //Controls whether the users'team has already had a boat or not
            else if (sender.team.boat != null)
            {
                response = ResponseMessageHelper.CreateResponse(HttpStatusCode.BadRequest, true, HttpStatusCode.BadRequest, ConstantResponse.TEAM_BOAT_ALREADY_HAVE_BOAT);
                return(Request.CreateResponse(HttpStatusCode.BadRequest, response));
            }

            //Adds the boat to the team
            Boat addedBoat = _repository.AddBoat(sender.id, boat);
            //Maps the added boat to the its DTO
            BoatDTO boatDTO = GenericMapper.MapToBoatDTO(addedBoat);

            response = ResponseMessageHelper.CreateResponse(HttpStatusCode.OK, false, boatDTO, ConstantResponse.TEAM_BOAT_SUCCESS);
            return(Request.CreateResponse <CustomResponse>(HttpStatusCode.OK, response));
        }