public async Task <ActionResult <List <QueryResult> > > PostAsync([FromBody] QueryResult queryResult) { Query q = Mapper.Map(queryResult.QueryObject); q.QueryTime = DateTime.Now; queryResult.QueryObject.Keywords = queryResult.QueryObject.Keywords.Where(k => !String.IsNullOrEmpty(k)).Select(k => k.ToLower()).ToList(); List <string> keywords = queryResult.QueryObject.Keywords; List <Restaurant> restaurants = Mapper.Map(queryResult.Restaurants).ToList(); try { //Add query to DB Qrepo.AddQuery(q); await Qrepo.SaveAsync(); } catch (Exception e) { logger.Error(e, e.ToString()); return(StatusCode(StatusCodes.Status400BadRequest)); } try { //Add any new keywords to the DB, and register all keywords to QueryKeywordJunction await Qrepo.AddQueryKeywordJunctionAsync(q.Id, keywords, (KeywordRepo)Krepo); await Qrepo.SaveAsync(); //Add any new restaurants to DB, and register any new keywords to existing restaurants await Rrepo.AddNewRestaurantsAsync(restaurants, keywords); await Qrepo.SaveAsync(); //Add query+restaurants to junction table await Qrepo.AddQueryRestaurantJunctionAsync(q.Id, restaurants, (RestaurantRepo)Rrepo); await Qrepo.SaveAsync(); } catch (Exception e) //defining Excpetion as e for debugging purposes even though it's unused and a code smell. { logger.Error(e, e.ToString()); return(StatusCode(StatusCodes.Status500InternalServerError)); } return(CreatedAtRoute("GetQueryResult", new { id = q.Id }, queryResult)); }
public async Task <ActionResult <QueryResult> > GetAsync(int id) { Query q; try { q = await Qrepo.GetQueryByIDAsync(id); } catch (Exception e) { logger.Error(e, e.ToString()); return(StatusCode(StatusCodes.Status400BadRequest)); } if (!(User.Identity.Name.Equals(q.Username) || User.IsInRole("admin"))) { return(StatusCode(403));//Forbidden } return(new QueryResult() { QueryObject = Mapper.Map(q), Restaurants = Mapper.Map(await Qrepo.GetRestaurantsInQueryAsync(q.Id)).ToList() }); }
public ActionResult <List <QueryResult> > Get() { List <Query> queryList; if (!User.IsInRole("admin")) //Admin gets full query list, other users only get their own { queryList = Qrepo.GetQueries().Where(q => q.Username.Equals(User.Identity.Name)).ToList(); } else { queryList = Qrepo.GetQueries().ToList(); } if (queryList == null) { return(StatusCode(StatusCodes.Status500InternalServerError)); } return(queryList.Select(m => new QueryResult() { QueryObject = Mapper.Map(m), Restaurants = Mapper.Map(Qrepo.GetRestaurantsInQuery(m.Id)).ToList() }).ToList()); }