コード例 #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;
            }
        }
コード例 #2
0
 //Makes the notification passive
 public void Update(Notification notification, bool passive)
 {
     notification.isActive = passive;
     DB.Notifications.Attach(notification);
     DB.Entry(notification).Property(x => x.isActive).IsModified = true;
     DB.SaveChanges();
 }
コード例 #3
0
        public void UpdateTeam(string email, int?teamId)
        {
            User user = GetByEmail(email);

            user.teamId = teamId;
            DB.Users.Attach(user);
            DB.Entry(user).Property(x => x.teamId).IsModified = true;
            DB.SaveChanges();
        }