Example #1
0
 public static User GetUserById(int id)
 {
     using (M32COMDBSERVER DB = new M32COMDBSERVER())
     {
         return(DB.Users.Include(b => b.team).Include(x => x.team.boat).Where(x => x.id == id).First());
     }
 }
Example #2
0
 public static Team GetUserTeamById(int id)
 {
     using (M32COMDBSERVER DB = new M32COMDBSERVER())
     {
         return(DB.Users.Include(b => b.team).Include(x => x.team.boat).Where(y => y.id == id).Select(x => x.team).SingleOrDefault());
     }
 }
Example #3
0
 public static User GetUserByEmail(string email)
 {
     using (M32COMDBSERVER DB = new M32COMDBSERVER())
     {
         return(DB.Users.Include(x => x.receivedNotification).Include(x => x.sentNotification).Include(x => x.team).Include(x => x.team.boat).First(x => x.email == email));
     }
 }
Example #4
0
        public HttpResponseMessage getNotifications()
        {
            var  token = Request.Headers.Authorization.Parameter;
            User user  = UserUtility.GetUserByToken(token);

            using (M32COMDBSERVER DB = new M32COMDBSERVER())
            {
                List <NotificationDTO> data     = _repository.getNotifications(user.id);
                CustomResponse         response = ResponseMessageHelper.CreateResponse(HttpStatusCode.OK, false, data, ConstantResponse.NOTIFICATION_ALL_SUCCESS);
                return(Request.CreateResponse <CustomResponse>(HttpStatusCode.OK, response));
            }
        }
Example #5
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;
            }
        }
Example #6
0
        public static Boolean IsEmailUnique(string email)
        {
            using (M32COMDBSERVER DB = new M32COMDBSERVER())
            {
                // email in use !
                if (DB.Users.Any(x => x.email == email))
                {
                    return(false);
                }

                // email is free!
                return(true);
            }
        }
Example #7
0
 // After an Action Runs
 public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
 {
     using (M32COMDBSERVER context = new M32COMDBSERVER())
     {
         context.Logs.Add(new Log()
         {
             isBefore   = false,
             logCaption = $"{actionExecutedContext.ActionContext.ControllerContext.ControllerDescriptor.ControllerName} - {actionExecutedContext.ActionContext.ActionDescriptor.ActionName}",
             time       = DateTime.Now,
             logDetail  = (actionExecutedContext.Response.Content as ObjectContent).ObjectType.FullName
         });
         context.SaveChanges();
     }
     base.OnActionExecuted(actionExecutedContext);
 }
Example #8
0
        public static User GetUserByEmailAndPassword(string email, string password)
        {
            using (M32COMDBSERVER DB = new M32COMDBSERVER())
            {
                var userSalt = DB.Users.Where(u => u.email == email).Select(u => u.passwordSalt).SingleOrDefault();

                if (userSalt == null)
                {
                    return(null);
                }

                string saltedPass = PasswordHashingUtility.GenerateSaltedHash(password, userSalt);

                return(DB.Users.Include(x => x.receivedNotification).Include(x => x.team).Where(x => x.email.Equals(email, StringComparison.OrdinalIgnoreCase) && x.password == saltedPass).FirstOrDefault());
            }
        }
Example #9
0
 // Before an Action Runs
 public override void OnActionExecuting(HttpActionContext actionContext)
 {
     using (M32COMDBSERVER context = new M32COMDBSERVER())
     {
         StringBuilder sb = new StringBuilder();
         foreach (var item in actionContext.ActionArguments)
         {
             sb.Append($"{item.Key}={item.Value.ToString()},");
         }
         context.Logs.Add(new Log()
         {
             isBefore   = true,
             logCaption = $"{actionContext.ControllerContext.ControllerDescriptor.ControllerName} - {actionContext.ActionDescriptor.ActionName}",
             time       = DateTime.Now,
             logDetail  = sb.ToString()
         });
         context.SaveChanges();
     }
     base.OnActionExecuting(actionContext);
 }
Example #10
0
        public HttpResponseMessage GetTeamMembers()
        {
            var            token  = Request.Headers.Authorization.Parameter;
            User           user   = UserUtility.GetUserByToken(token);
            Nullable <int> teamId = user.teamId;
            CustomResponse response;

            using (M32COMDBSERVER DB = new M32COMDBSERVER())
            {
                if (teamId != null)
                {
                    IEnumerable <UserDTO> result = _repository.GetTeamMembers(teamId);

                    response = ResponseMessageHelper.CreateResponse(HttpStatusCode.OK, false, result, ConstantResponse.OK);
                    return(Request.CreateResponse <CustomResponse>(HttpStatusCode.OK, response));
                }
                //returns null if the user does not have a team
                response = ResponseMessageHelper.CreateResponse(HttpStatusCode.OK, false, null, ConstantResponse.TEAM_GET_MEMBERS);
                return(Request.CreateResponse <CustomResponse>(HttpStatusCode.OK, response));
            }
        }
Example #11
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));
            }
        }