Exemplo n.º 1
0
        public HttpResponseMessage Create([FromBody] Team team)
        {
            //Gets the user from token
            var token = Request.Headers.Authorization.Parameter;

            string userEmail = UserUtility.GetEmailByToken(token);

            CustomResponse response;

            using (M32COMDBSERVER DB = new M32COMDBSERVER())
            {
                //Returns error if team name is not unique
                if (!_repository.IsTeamNameUnique(team.name))
                {
                    response = ResponseMessageHelper.CreateResponse(HttpStatusCode.BadRequest, true, null, ConstantResponse.TEAM_FAILED);
                    return(Request.CreateResponse <CustomResponse>(HttpStatusCode.BadRequest, response));
                }

                //Returns error if the user has already had a team
                bool userHasTeam = _repository.UserHasTeam(userEmail);
                if (userHasTeam)
                {
                    response = ResponseMessageHelper.CreateResponse(HttpStatusCode.BadRequest, true, null, ConstantResponse.HAS_TEAM_ERR);
                    return(Request.CreateResponse <CustomResponse>(HttpStatusCode.BadRequest, response));
                }

                //Inserts the team
                Team insertedTeam = _repository.InsertTeam(userEmail, team);

                //Maps the team to its DTO
                TeamDTO teamDTO = GenericMapper.MapToTeamDTO(insertedTeam);

                response = ResponseMessageHelper.CreateResponse(HttpStatusCode.Created, false, teamDTO, ConstantResponse.TEAM_CREATED);
                return(Request.CreateResponse <CustomResponse>(HttpStatusCode.Created, response));
            }
        }