public static string GenerateOutput(List <BeerArray> listOfBeers, List <BeerDto> listOfRatings) { string jsonRatings = null; BeerRatings beerRatings = new BeerRatings(); List <BeerRatings> listOfBeerRatings = new List <BeerRatings>(); try { foreach (BeerArray eachBeer in listOfBeers) { beerRatings.Id = listOfBeers[0].id; beerRatings.name = listOfBeers[0].name; beerRatings.description = listOfBeers[0].description; beerRatings.userRatings = listOfRatings; listOfBeerRatings.Add(beerRatings); } jsonRatings = JsonConvert.SerializeObject(listOfBeerRatings); } catch { //TODO Error } return(jsonRatings); }
public List <BeerRatings> GetBeersRatings(string path) { List <BeerRatings> beerRatings = new List <BeerRatings>(); var json = JsonHelper.Get(path); if (!string.IsNullOrEmpty(json)) { beerRatings = BeerRatings.FromJson(json); } return(beerRatings); }
public async Task <ActionResult <string> > AddBeer([FromBody] UserRating value, int id) { try { string json = string.Empty; List <Beer> bears = await _punkApiService.GetBeers(_conf.PunkAPIUrl); var result = bears.Where(x => x.Id == id); if (result == null) { return("Beer Id not exist"); } beerRatings = new List <BeerRatings>(); BeerRatings beerRating = new BeerRatings(); beerRating.UserRatings = new List <UserRating>(); beerRatings = _punkApiService.GetBeersRatings(path); if (beerRatings != null) { beerRating = beerRatings.Where(x => x.Id == id).FirstOrDefault(); if (beerRating != null) { beerRating.UserRatings.Add(value); } else { beerRating = new BeerRatings(); beerRating.UserRatings = new List <UserRating>(); beerRating.Id = id; beerRating.UserRatings.Add(value); beerRatings.Add(beerRating); } } else { beerRating.Id = id; beerRating.UserRatings.Add(value); beerRatings.Add(beerRating); } json = _punkApiService.AddBeerRating(beerRatings, path); return(Ok(json)); } catch (Exception ex) { throw ex; } }
/// <summary> /// Initialization for Beer Schema for Mutation /// </summary> /// <param name="schema"></param> private static void InitializeMutationSchema(GraphQLSchema <BeerContext> schema) { schema.AddMutation("addBeer", new { id = 0, name = string.Empty, averageRatings = 0, beerTypeId = 0 }, (db, args) => { var newBeer = new Beers { Name = args.name, BeerTypeId = args.beerTypeId, AverageRatings = args.averageRatings }; db.Beers.Add(newBeer); db.SaveChanges(); return(newBeer.Id); }, (db, args, id) => db.Beers.AsQueryable().FirstOrDefault(a => a.Id == id)); schema.AddMutation("putBeer", new { id = 0, name = string.Empty, averageRatings = 0, beerTypeId = 0 }, (db, args) => { var oldBeer = db.Beers.Find(args.id); if (oldBeer != null) { oldBeer.Name = args.name; oldBeer.BeerTypeId = args.beerTypeId; oldBeer.AverageRatings = args.averageRatings; db.Entry(oldBeer).State = EntityState.Modified; db.SaveChanges(); return(oldBeer.Id); } return(0); }, (db, args, id) => db.Beers.AsQueryable().FirstOrDefault(a => a.Id == id)); schema.AddMutation("deleteBeer", new { id = 0 }, (db, args) => { var oldBeer = db.Beers.Find(args.id); if (oldBeer != null) { db.Beers.Remove(oldBeer); db.SaveChanges(); return(oldBeer.Id); } return(0); }, (db, args, id) => db.Beers.AsQueryable().FirstOrDefault(a => a.Id == id)); schema.AddMutation("addBeerRating", new { id = 0, rating = 0, beerId = 0 }, (db, args) => { var beerRating = new BeerRatings { Id = args.id, Rating = args.rating, BeerId = args.beerId }; db.BeerRatings.Add(beerRating); db.SaveChanges(); var allRatings = db.BeerRatings.Where(r => r.BeerId == beerRating.BeerId).Select(r => r.Rating).ToList(); double averageRating = allRatings.Average(); var bee = db.Beers.FirstOrDefault(b => b.Id == beerRating.BeerId); if (bee != null) { bee.AverageRatings = averageRating; db.Entry(bee).State = EntityState.Modified; db.SaveChanges(); return(bee.Id); } return(0); }, (db, args, id) => db.Beers.AsQueryable().FirstOrDefault(a => a.Id == id)); }