Exemplo n.º 1
0
        public HttpResponseMessage Quit()
        {
            //Gets the user from token
            var            token     = Request.Headers.Authorization.Parameter;
            string         userEmail = UserUtility.GetEmailByToken(token);
            CustomResponse response;



            try
            {
                using (M32COMDBSERVER DB = new M32COMDBSERVER())
                {
                    User loginUser = DB.Users.Include(x => x.receivedNotification).Include(x => x.team).Include(x => x.team.boat).First(x => x.email == userEmail);
                    if (loginUser.team == null)
                    {
                        response = ResponseMessageHelper.CreateResponse(HttpStatusCode.BadRequest, true, null, ConstantResponse.TEAM_QUIT_FAILED);
                        return(Request.CreateResponse <CustomResponse>(HttpStatusCode.BadRequest, response));
                    }
                    Team team = DB.Teams.Include(x => x.teamMembers).Where(x => x.id == loginUser.teamId).First();

                    //Team leader disposes the team
                    if (team.leaderId == loginUser.id)
                    {
                        DB.Teams.Remove(team);
                        DB.SaveChanges();
                        response = ResponseMessageHelper.CreateResponse(HttpStatusCode.OK, false, null, ConstantResponse.TEAM_DISPOSED);
                        return(Request.CreateResponse <CustomResponse>(HttpStatusCode.OK, response));
                    }
                    //Person quits from the team
                    team.teamMembers.Remove(loginUser);
                    DB.Entry(team).State      = EntityState.Modified;
                    DB.Entry(loginUser).State = EntityState.Modified;
                    DB.SaveChanges();
                    response = ResponseMessageHelper.CreateResponse(HttpStatusCode.OK, false, ConstantResponse.OK, ConstantResponse.TEAM_QUIT_SUCCESS);
                    return(Request.CreateResponse <CustomResponse>(HttpStatusCode.OK, response));
                }
            }
            catch (DbEntityValidationException e)
            {
                foreach (var eve in e.EntityValidationErrors)
                {
                    Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                                      eve.Entry.Entity.GetType().Name, eve.Entry.State);
                    foreach (var ve in eve.ValidationErrors)
                    {
                        Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
                                          ve.PropertyName, ve.ErrorMessage);
                    }
                }
                throw;
            }
        }
Exemplo n.º 2
0
        public HttpResponseMessage RespondInvitation([FromUri] bool accept)
        {
            //Gets the headers
            var            headers = Request.Headers;
            CustomResponse response;

            //if http packet does not include notification Token
            if (!headers.Contains("Actiontoken"))
            {
                response = ResponseMessageHelper.CreateResponse(HttpStatusCode.BadRequest, true, null, ConstantResponse.TEAM_RESPOND_MISSING_ACTIONTOKEN);
                return(Request.CreateResponse <CustomResponse>(HttpStatusCode.BadRequest, response));
            }

            //Notification token
            var actionToken = headers.GetValues("Actiontoken").First();


            //Gets the Receiver from Auth token
            var token = Request.Headers.Authorization.Parameter;

            //Gets the user who sent the request
            string loginUserEmail = UserUtility.GetEmailByToken(token);
            User   loginUser      = _userRepository.GetByEmail(loginUserEmail);


            //Gets the related notification from db by using unique actionToken
            Notification notification = _notificationRepository.getByToken(actionToken);

            //invalid notification check
            if (notification == null || !notification.isActive || notification.receivedBy.id != loginUser.id)
            {
                response = ResponseMessageHelper.CreateResponse(HttpStatusCode.Forbidden, true, null, ConstantResponse.TEAM_RESPOND_INVALID_NOTIFICATION);
                return(Request.CreateResponse <CustomResponse>(HttpStatusCode.Forbidden, response));
            }


            //Rejection check
            if (!accept)
            {
                response = ResponseMessageHelper.CreateResponse(HttpStatusCode.OK, false, ConstantResponse.REJECTED, ConstantResponse.TEAM_RESPOND_REJECTED);
            }
            else
            {
                response = ResponseMessageHelper.CreateResponse(HttpStatusCode.OK, false, ConstantResponse.ACCEPTED, ConstantResponse.TEAM_RESPOND_ACCEPTED);
                //receiver is added to the team where sender is the leader
                _repository.AcceptTeamInvitation(loginUserEmail, notification.sentBy.teamId);
                //Makes notification passive
            }
            _notificationRepository.Update(notification, false);
            return(Request.CreateResponse <CustomResponse>(HttpStatusCode.OK, response));
        }
        public HttpResponseMessage Apply(int competitionId)
        {
            var    token     = Request.Headers.Authorization.Parameter;
            string userEmail = UserUtility.GetEmailByToken(token);

            User user = _userRepository.GetByEmail(userEmail);

            CustomResponse response;

            //checks whether user is the team leader
            if (user.team != null && user.team.leaderId == user.id)
            {
                Competition comp = _repository.GetById(competitionId);

                //Null check for competition
                if (comp == null)
                {
                    response = ResponseMessageHelper.CreateResponse(HttpStatusCode.NotFound, true, HttpStatusCode.NotFound, ConstantResponse.COMPETITITON_APPLIED_NO_COMP_FOUND);
                    return(Request.CreateResponse <CustomResponse>(HttpStatusCode.NotFound, response));
                }

                //Checks whether competition is active or not
                else if (comp.endDate < DateTime.Now)
                {
                    response = ResponseMessageHelper.CreateResponse(HttpStatusCode.BadRequest, true, HttpStatusCode.BadRequest, ConstantResponse.COMPETITION_APPLIED_NOT_ACTIVE);
                    return(Request.CreateResponse <CustomResponse>(HttpStatusCode.BadRequest, response));
                }

                //Checks whether the team has already applied or not
                bool teamAlreadyApplied = _repository.HasAlreadyApplied(competitionId, user.teamId);
                if (teamAlreadyApplied)
                {
                    response = ResponseMessageHelper.CreateResponse(HttpStatusCode.BadRequest, true, HttpStatusCode.BadRequest, ConstantResponse.COMPETITION_APPLIED_ALREADY);
                    return(Request.CreateResponse <CustomResponse>(HttpStatusCode.BadRequest, response));
                }

                //Adds the application to the DB
                _repository.Apply(comp.id, user.team.id);

                response = ResponseMessageHelper.CreateResponse(HttpStatusCode.OK, false, HttpStatusCode.OK, ConstantResponse.COMPETITION_APPLIED_SUCCESS);
                return(Request.CreateResponse <CustomResponse>(HttpStatusCode.OK, response));
            }
            response = ResponseMessageHelper.CreateResponse(HttpStatusCode.BadRequest, true, HttpStatusCode.BadRequest, ConstantResponse.COMPETITION_APPLIED_NOT_TEAMLEADER);
            return(Request.CreateResponse <CustomResponse>(HttpStatusCode.BadRequest, response));
        }
Exemplo n.º 4
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));
            }
        }