Exemplo n.º 1
0
        public HttpResponseMessage ListReddits(HttpRequestMessage request, string token = null)
        {
            HttpResponseMessage respose = new HttpResponseMessage();

            if (string.IsNullOrEmpty(token))
            {
                return(request.CreateErrorResponse(HttpStatusCode.Unauthorized, "No Token Supplied. Please register or login"));
            }
            var svc     = new RedditService();
            var reddits = svc.GetAllReddits();

            return(request.CreateResponse(HttpStatusCode.OK, reddits));
        }
Exemplo n.º 2
0
        public HttpResponseMessage AddFavorite(HttpRequestMessage request, string token, [FromBody] Favorite favorite)
        {
            try
            {
                //var fav  = new Favorite
                //{
                //    RedditId = "87y0ei",
                //    Tags = new List<string>
                //    {
                //        {"sasa" }, {"when"}
                //    }
                //};

                //return request.CreateResponse(HttpStatusCode.OK, fav);



                var svc      = new RedditService();
                var existing = svc.GetUserByToken(token);

                if (existing == null)
                {
                    return(request.CreateErrorResponse(HttpStatusCode.NotFound, "Invalid access token"));
                }

                var reddits = svc.GetAllReddits();

                var reddit = reddits.FirstOrDefault(x => x.RedditId == favorite.RedditId);

                if (reddit == null)
                {
                    return(request.CreateErrorResponse(HttpStatusCode.NotFound, $"Reddit Id {favorite.RedditId} not Found"));
                }


                if (existing.Favorites == null)
                {
                    existing.Favorites = new List <Favorite>();
                }

                existing.Favorites.Add(favorite);
                svc.SaveUser(existing);

                return(request.CreateResponse(HttpStatusCode.OK, $"Your tages for {favorite.RedditId} has been saved"));
            }
            catch (Exception ex)
            {
                return(request.CreateErrorResponse(HttpStatusCode.BadRequest, "Bad Request " + ex.Message));
            }
        }