Esempio n. 1
0
        public async Task <IActionResult> PutAsync(string username, [FromBody] UserModel value)
        {
            AppUser updateVariable;

            if (!username.Equals(value.Username))
            {
                return(StatusCode(StatusCodes.Status400BadRequest));
            }
            if (!(User.Identity.Name.Equals(username) || User.IsInRole("admin")))
            {
                return(StatusCode(403));//Forbidden
            }
            updateVariable = Mapper.Map(value);

            try
            {
                Arepo.UpdateUser(updateVariable);
            }
            catch (Exception e)
            {
                logger.Error(e, e.ToString());
                return(StatusCode(StatusCodes.Status400BadRequest));
            }
            try
            {
                await Arepo.SaveAsync();
            }
            catch (Exception e)
            {
                logger.Error(e, e.ToString());
                return(StatusCode(StatusCodes.Status500InternalServerError));
            }
            return(StatusCode(StatusCodes.Status204NoContent));
        }
Esempio n. 2
0
        public async Task <IActionResult> CreateAsync([FromBody] UserModel value)
        {
            AppUser createVariable;

            createVariable = Mapper.Map(value);

            try
            {
                await Arepo.AddUserAsync(createVariable);
            }

            catch (Exception e)
            {
                logger.Error(e, e.ToString());
                return(StatusCode(StatusCodes.Status400BadRequest));
            }
            try
            {
                await Arepo.SaveAsync();
            }
            catch (Exception e)
            {
                logger.Error(e, e.ToString());
                return(StatusCode(StatusCodes.Status500InternalServerError));
            }
            return(CreatedAtRoute("GetUser", new { username = value.Username }, value));
        }
Esempio n. 3
0
 public ActionResult <List <string> > Get(string rId)
 {
     try
     {
         return(Arepo.GetUsers(true).Where(a => a.Blacklist.Any(f => f.RestaurantId.Equals(rId))).Select(a => a.Username).ToList());
     }
     catch (Exception e)
     {
         logger.Error(e, e.ToString());
         return(StatusCode(StatusCodes.Status500InternalServerError));
     }
 }
Esempio n. 4
0
 public async Task <ActionResult <bool> > GetAsync(string rId)
 {
     //Since this method is authorized by Identity, it will automatically handle returning 401 if user isn't logged in.
     try
     {
         return((await Arepo.GetBlacklistForUserAsync(User.Identity.Name)).Any(n => n.Id.Equals(rId)));
     }
     catch (Exception e)
     {
         logger.Error(e, e.ToString());
         return(StatusCode(StatusCodes.Status500InternalServerError));
     }
 }
Esempio n. 5
0
 public async Task <ActionResult <List <RestaurantModel> > > GetAsync()
 {
     //Since this method is authorized by Identity, it will automatically handle returning 401 if user isn't logged in.
     try
     {
         return(Mapper.Map(await Arepo.GetBlacklistForUserAsync(User.Identity.Name)).ToList());
     }
     catch (Exception e)
     {
         logger.Error(e, e.ToString());
         return(StatusCode(StatusCodes.Status500InternalServerError));
     }
 }
Esempio n. 6
0
        public ActionResult <List <RestaurantModel> > Get(string search)
        {
            Rrepo.GetRestaurants(true);
            string [] stringarray = search.Split(new Char[] { ' ' });
            List <List <RestaurantModel> > Listoflists = new List <List <RestaurantModel> >();

            foreach (var word in stringarray)
            {
                try
                {
                    Listoflists.Add(Rrepo.GetRestaurants(true).Where(k => k.RestaurantKeywordJunction.Any(rkj => rkj.Word.Equals(word))).Select(k => Mapper.Map(k)).ToList());
                    Listoflists.Add(Rrepo.GetRestaurants(true).Where(k => k.Name.Contains(word)).Select(k => Mapper.Map(k)).ToList());
                }
                catch (DbUpdateException ex)
                {
                    logger.Error(ex, ex.ToString());
                    return(StatusCode(StatusCodes.Status500InternalServerError));
                }
            }

            List <RestaurantModel> results = new List <RestaurantModel>();

            foreach (var list in Listoflists)
            {
                foreach (var restaurant in list)
                {
                    //output each restaurant from each list that matches what was searched
                    if (User.Identity.IsAuthenticated)
                    {
                        if (!Arepo.GetBlacklistForUser(User.Identity.Name).Any(c => c.Id.Equals(restaurant.Id)) && !results.Contains(restaurant))
                        {
                            results.Add(restaurant);
                        }
                    }
                    else
                    {
                        if (!results.Contains(restaurant))
                        {
                            results.Add(restaurant);
                        }
                    }
                }
            }

            return(results);
        }
Esempio n. 7
0
        [Authorize(Roles = "admin")]//checking if you are in some role, to access something
        public ActionResult <List <UserModel> > Get()
        {
            List <AppUser> userlist;

            try
            {
                userlist = Arepo.GetUsers().ToList();
            }
            catch (Exception e)
            {
                logger.Error(e, e.ToString());
                return(StatusCode(StatusCodes.Status500InternalServerError));
            }
            if (userlist == null)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError));
            }

            return(Mapper.Map(userlist).ToList());
        }
 public async Task <ActionResult <List <FrequencyWrapper <RestaurantModel> > > > GetAsync(string username)
 {
     if (!(await Arepo.DBContainsUsernameAsync(username)))
     {
         return(StatusCode(StatusCodes.Status400BadRequest));
     }
     try
     {
         return(Rrepo.GetRestaurants(true).Select(r => new FrequencyWrapper <RestaurantModel>()
         {
             Obj = Mapper.Map(r),
             Frequency = r.QueryRestaurantJunction.Count(q => q.Query.Username.Equals(username))
         }).OrderByDescending(k => k.Frequency).ToList());
     }
     catch (Exception e)
     {
         logger.Error(e, e.ToString());
         return(StatusCode(StatusCodes.Status500InternalServerError));
     }
 }
Esempio n. 9
0
 public async Task <IActionResult> DeleteAsync(string value)
 {
     try
     {
         await Arepo.RemoveRestaurantFromBlacklistAsync(User.Identity.Name, value, (RestaurantRepo)Rrepo);
     }
     catch (Exception e)
     {
         logger.Error(e, e.ToString());
         return(StatusCode(StatusCodes.Status400BadRequest));
     }
     try
     {
         await Rrepo.SaveAsync();
     }
     catch (Exception e)
     {
         logger.Error(e, e.ToString());
         return(StatusCode(StatusCodes.Status500InternalServerError));
     }
     return(StatusCode(StatusCodes.Status204NoContent));
 }
Esempio n. 10
0
 public async Task <IActionResult> CreateAsync([FromBody] StringModel sm)
 {
     try
     {
         await Arepo.AddRestaurantToFavoritesAsync(User.Identity.Name, sm.Value, (RestaurantRepo)Rrepo);
     }
     catch (Exception e)
     {
         logger.Error(e, e.ToString());
         return(StatusCode(StatusCodes.Status400BadRequest));
     }
     try
     {
         await Rrepo.SaveAsync();
     }
     catch (Exception e)
     {
         logger.Error(e, e.ToString());
         return(StatusCode(StatusCodes.Status500InternalServerError));
     }
     return(StatusCode(StatusCodes.Status204NoContent));
 }
Esempio n. 11
0
        public async Task <ActionResult <UserModel> > GetByUsernameAsync(string username)
        {
            if (User == null)
            {
                return(StatusCode(401));//unauthorized, in case User is null for some reason like the tests.
            }
            if (!(User.Identity.Name.Equals(username) || User.IsInRole("admin")))
            {
                return(StatusCode(403));//Forbidden
            }
            AppUser userVariable;

            try
            {
                userVariable = await Arepo.GetUserByUsernameAsync(username);
            }

            catch (Exception e)
            {
                logger.Error(e, e.ToString());
                return(StatusCode(StatusCodes.Status400BadRequest));
            }
            return(Mapper.Map(userVariable));
        }