Ejemplo n.º 1
0
        public async Task <IHttpActionResult> RemoveRating(string moviename, [FromBody] RemoveRatingModel remove)
        {
            await Task.Yield();

            if (moviename == null || remove == null)
            {
                return(BadRequest("Nor movie nor rating can be empty"));
            }
            var target = ConfigurationManager.AppSettings["movie"];

            target += $"/{moviename}/rating";
            var request   = new HttpRequestMessage(HttpMethod.Delete, target);
            var authToken = Request.Headers.Authorization;

            request.Headers.Authorization = authToken;
            var jsonContent   = JsonConvert.SerializeObject(remove);
            var stringContent = new StringContent(jsonContent, UnicodeEncoding.UTF8, "application/json");

            request.Content = stringContent;
            var response = await client.SendAsync(request);

            var returned = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

            JavaScriptSerializer jsonSerializer = new JavaScriptSerializer();
            var contentJson = jsonSerializer.DeserializeObject(returned);

            if (response.IsSuccessStatusCode)
            {
                return(Ok(contentJson));
            }
            else
            {
                return(Content(response.StatusCode, contentJson));
            }
        }
Ejemplo n.º 2
0
        public async Task <IHttpActionResult> RemoveRating(string moviename, [FromBody] RemoveRatingModel remove)
        {
            await Task.Yield();

            if (moviename == null || remove == null)
            {
                return(BadRequest("Movie or username cant be empty"));
            }
            var token         = Request.Headers.Authorization.ToString();
            var isCorrectUser = CheckIfSessionIsCorrect(remove.NickName, token);

            if (!isCorrectUser)
            {
                return(ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.Forbidden, "You cant remove other users vote")));
            }
            var wasRemoved = movieLogic.RemoveRating(moviename, remove.NickName);

            if (!wasRemoved)
            {
                return(ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.NotFound, "The movie does not exist in our servers")));
            }
            return(Ok("Vote removed"));
        }