public async Task <AddToFavouritesResponse> AddToFavourites(AddToFavouritesRequest item)
        {
            string url         = $"/item/add-fav";
            var    requestBody = await Task.Run(() => JsonConvert.SerializeObject(item));

            using (HttpClient httpClient = new HttpClient())
            {
                AddToFavouritesResponse data = new AddToFavouritesResponse();
                try
                {
                    var authHeader = new AuthenticationHeaderValue("Bearer", await SecureStorage.GetAsync("auth_token"));
                    httpClient.DefaultRequestHeaders.Authorization = authHeader;
                    httpClient.BaseAddress = new Uri(Constants.BaseUrl);
                    StringContent       content = new StringContent(requestBody, Encoding.UTF8, "application/json");
                    HttpResponseMessage result  = await httpClient.PostAsync(url, content);

                    string response = await result.Content.ReadAsStringAsync();

                    data = JsonConvert.DeserializeObject <AddToFavouritesResponse>(response);

                    if (result.IsSuccessStatusCode && result.StatusCode == HttpStatusCode.OK)
                    {
                        return(data);
                    }

                    return(null);
                }
                catch (Exception exp)
                {
                    return(null);
                }
            }
        }
Exemple #2
0
        public HttpResponseMessage AddToFavourites()
        {
            string UserId    = HttpContext.Current.Request.Form["UserId"];
            string CompanyId = HttpContext.Current.Request.Form["CompanyId"];
            AddToFavouritesResponse _Response = new AddToFavouritesResponse();

            _Response.Flag = "false";

            if (!string.IsNullOrEmpty(UserId) && !string.IsNullOrEmpty(CompanyId))
            {
                var uid = Convert.ToInt32(UserId);
                var cid = Convert.ToInt32(CompanyId);
                var uv  = _db.UserFavorites.FirstOrDefault(u => u.UserId == uid && u.CompanyId == cid);
                if (null == uv)
                {
                    UserFavorite _UserFavorite = new UserFavorite();
                    _UserFavorite.CompanyId = Convert.ToInt32(CompanyId);
                    _UserFavorite.UserId    = Convert.ToInt64(UserId);
                    _db.UserFavorites.Add(_UserFavorite);
                    _db.SaveChanges();
                    _Response.MESSAGE = "Added to favorite";
                    _Response.Flag    = "true";
                }
                else
                {
                    _db.UserFavorites.Remove(uv);
                    _db.SaveChanges();
                    _Response.Flag    = "true";
                    _Response.MESSAGE = "Company removed from favorites";
                }
            }
            else
            {
                _Response.Flag = "Insufficient data";
            }

            return(Request.CreateResponse(HttpStatusCode.OK, _Response));
        }