예제 #1
0
 public ActionResult Feed([FromQuery] int p = 0, int n = 10)
 {
     using (var context = new InstacramDbContext()) {
         return(Ok(new {
             posts = context.GetUserFeed(Int32.Parse(this.User.Identity.Name), p, n)
         }));
     }
 }
예제 #2
0
        public Object Authenticate(string username, string password)
        {
            using (var context = new InstacramDbContext()) {
                var user = context.Users.SingleOrDefault(x => x.Username == username && x.Password == password);
                // return null if user not found
                if (user == null)
                {
                    return(null);
                }

                // authentication successful so generate jwt token
                string strToken = GenerateToken(user);
                return(new { token = strToken });
            }
        }
예제 #3
0
 public ActionResult Comment([FromBody] PostComment comment, [FromQuery] int id = -1)
 {
     using (var context = new InstacramDbContext()) {
         if (comment.Comment == null || id == -1)
         {
             return(BadRequest("Malformed Request"));
         }
         if (context.GetPostById(id) == null)
         {
             return(NotFound("Post Not Found"));
         }
         context.AddComment(Int32.Parse(this.User.Identity.Name), id, comment.Comment);
         return(Ok("Success"));
     }
 }
예제 #4
0
 public ActionResult Put([FromBody] User user)
 {
     using (var context = new InstacramDbContext()) {
         if ((user.Email ?? user.Name ?? user.Password) == null)
         {
             return(BadRequest("Malformed user object"));
         }
         context.EditUser(
             Int32.Parse(this.User.Identity.Name),
             user.Password,
             user.Email,
             user.Name);
         return(Ok("Success"));
     }
 }
예제 #5
0
 public ActionResult Get([FromQuery] int id = -1)
 {
     using (var context = new InstacramDbContext()) {
         if (id == -1)
         {
             return(BadRequest("Malformed Request"));
         }
         var post = context.GetPostById(id);
         if (post == null)
         {
             return(NotFound("Post Not Found"));
         }
         return(Ok(context.GetFormattedPost(post)));
     }
 }
예제 #6
0
 public ActionResult Unfollow([FromQuery] string username = null)
 {
     using (var context = new InstacramDbContext()) {
         if (username == null)
         {
             return(BadRequest("Malformed Request"));
         }
         var user = context.GetUserByUsername(username);
         if (user == null)
         {
             return(Ok());
         }
         context.RemoveFollow(Int32.Parse(this.User.Identity.Name), user.Id);
         return(Ok("Success"));
     }
 }
예제 #7
0
 public ActionResult Unlike([FromQuery] int id = -1)
 {
     using (var context = new InstacramDbContext()) {
         if (id == -1)
         {
             return(BadRequest("Malformed Request"));
         }
         var post = context.GetPostById(id);
         if (post == null)
         {
             return(NotFound("Post Not Found"));
         }
         context.RemoveLike(Int32.Parse(this.User.Identity.Name), id);
         return(Ok("Success"));
     }
 }
예제 #8
0
 public ActionResult Post([FromBody] Post post)
 {
     using (var context = new InstacramDbContext()) {
         if (post.Description_text == null || post.Src == null)
         {
             return(BadRequest("Malformed Request"));
         }
         return(Ok(new {
             post_id = context.AddPost(
                 Int32.Parse(this.User.Identity.Name),
                 post.Description_text,
                 post.Src,
                 post.Thumbnail
                 ).Id
         }));
     }
 }
예제 #9
0
        public Object Register(string username, string password, string email, string name)
        {
            using (var context = new InstacramDbContext()) {
                var user = context.GetUserByUsername(username);
                // return null if user not found
                if (user != null)
                {
                    return(null);
                }

                // register new user
                // user = new User (username, password, email, name);
                // context.Users.Add (user);
                // context.SaveChanges ();
                user = context.AddUser(username, password, email, name);
                string strToken = GenerateToken(user);
                return(new { token = strToken });
            }
        }
예제 #10
0
 public ActionResult Get([FromQuery] int id = -1, string username = null)
 {
     using (var context = new InstacramDbContext()) {
         if (id == -1 && username == null)
         {
             return(Ok(context.GetFormattedUser(Int32.Parse(this.User.Identity.Name))));
         }
         if (id != -1 && username != null)
         {
             return(BadRequest("Malformed Request"));
         }
         var user = context.GetUserById(id) ?? context.GetUserByUsername(username);
         if (user == null)
         {
             return(NotFound("User Not Found"));
         }
         return(Ok(context.GetFormattedUser(user.Id)));
     }
 }
예제 #11
0
 public ActionResult Put([FromBody] Post post, [FromQuery] int id = -1)
 {
     using (var context = new InstacramDbContext()) {
         if (id == -1 || (post.Description_text == null && post.Src == null))
         {
             return(BadRequest("Malformed Request"));
         }
         var the_post = context.GetPostById(id);
         if (the_post == null)
         {
             return(NotFound("Post Not Found"));
         }
         if (the_post.AuthorId != Int32.Parse(this.User.Identity.Name))
         {
             return(Unauthorized("You Are Unauthorized To Make That Request"));
         }
         context.EditPost(id, post.Description_text, post.Src);
         return(Ok("Success"));
     }
 }
예제 #12
0
 public ActionResult Delete([FromQuery] int id = -1)
 {
     using (var context = new InstacramDbContext()) {
         if (id == -1)
         {
             return(BadRequest("Malformed Request"));
         }
         var post = context.GetPostById(id);
         if (post == null)
         {
             return(NotFound("Post Not Found"));
         }
         if (post.AuthorId != Int32.Parse(this.User.Identity.Name))
         {
             return(Unauthorized("You Are Unauthorized To Make That Request"));
         }
         context.RemovePost(id);
         return(Ok("Success"));
     }
 }