public async Task <IActionResult> CreateCar([FromBody] CarResource carResource) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var car = mapper.Map <CarResource, Car>(carResource); car.Race = context.Races.Where(r => r.Id == car.RaceId).FirstOrDefault(); var sections = new List <CourseSection>(); foreach (var checkpoint in context.Checkpoints.Where(c => c.RaceId == car.RaceId).ToList()) { sections.Add( new CourseSection() { Car = car, Checkpoint = checkpoint } ); } car.CourseSections = sections; repository.Add(car); await unitOfWork.CompleteAsync(); var finalResource = mapper.Map <Car, CarResource>(car); return(Ok(finalResource)); }
public IEnumerable <CarResource> GetCars() { List <CarResource> carResList = new List <CarResource>(); foreach (Car c in usercntxt.Cars.Include("User").ToList()) { CarResource cres = new CarResource { Id = c.CarId, Brand = c.Brand, Model = c.Model, PropietaryId = c.User.UserId }; carResList.Add(cres); } return(carResList); }
public async Task <IActionResult> UpdateCar(int id, [FromBody] CarResource carResource) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var car = await repository.GetCar(id); if (car != null) { mapper.Map <CarResource, Car>(carResource, car); } else { return(NotFound()); } await unitOfWork.CompleteAsync(); return(Ok(car)); }
public CarResource Read(XmlReader xml) { CarResource car; switch (xml.LocalName) { case "ordinary-car": car = new CarResource(); break; case "racecar": car = new RaceCarResource(); break; case "super_duper": car = new SuperDuperUltraRaceCarResource(); break; default: throw new XmlException(); } XmlReader sub = xml.ReadSubtree(); car.ReadFrom(sub); sub.Close(); return(car); }
private string PrepareResource(CarResource res) { string content = null; List<string> prelines = new List<string>(); List<string> paramlines = new List<string>(); List<string> postlines = new List<string>(); bool paramfound = false; bool paramfinished = false; int[] lastparamcolumn = null; using (StreamReader reader = new StreamReader(Path.Combine(ProjectDir, CarModelDir, res.FileName))) { while (!reader.EndOfStream) { string line = reader.ReadLine(); if (!paramfound) { prelines.Add(line); } else if (paramfinished) { postlines = new List<string>(); } else { string tmp = line.Trim(); if (tmp.StartsWith("{")) { lastparamcolumn = ProcessParamColumn(line); paramlines.Add(line); } else if (tmp.StartsWith("'")) { paramlines.Add(line); } else { paramfinished = true; postlines.Add(line); } } if (line == "[PARAMETER]") { paramfound = true; } } } StringBuilder b = new StringBuilder(); using (StringWriter writer = new StringWriter(b)) { foreach (string s in prelines) { writer.WriteLine(s); } foreach (string s in paramlines) { writer.WriteLine(ProcessParamLine(s, res.Parameters, lastparamcolumn)); } foreach (string s in postlines) { writer.WriteLine(s); } } return b.ToString(); }
private List<CarResource> CollectCarResources(List<CyPhy.CarModel> carcomps) { List<CarResource> result = new List<CarResource>(); foreach (var comp in carcomps) { foreach (var res in comp.Children.CarResourceCollection) { // Is the resource already listed? CarResource currres = null; var existingres = result.Where(r => r.FileName == res.Attributes.ResourcePath); if (existingres.Any()) { if (existingres.Count() > 1) { throw new Exception("Internal error: car resource found more than once."); } currres = existingres.First(); } else { currres = new CarResource() { FileName = res.Attributes.ResourcePath }; result.Add(currres); } foreach (var param in res.SrcConnections.CarResourceParameterCollection) { if (param.SrcEnds.CarParameter != null) { if (param.SrcEnds.CarParameter.Attributes.Value.Length == 0) { GMEConsole.Warning.WriteLine("Empty parameter value for '" + param.SrcEnds.CarParameter.Name + "', ignoring."); } else { var existingparam = currres.Parameters.Where(p => p.Name == param.SrcEnds.CarParameter.Name); if (existingparam.Any()) { GMEConsole.Warning.WriteLine("Parameter '" + existingparam.First().Name + "' set twice on resource '" + currres.FileName + "'. Ignoring second setting."); } else { var newparam = new CarFileParam() { Name = param.SrcEnds.CarParameter.Name, Value = param.SrcEnds.CarParameter.Attributes.Value, Replacement = ((param.Attributes.ParameterKey.Length > 0) ? AdamsCarFileReplacement.FromString(param.Attributes.ParameterKey) : null) }; currres.Parameters.Add(newparam); } } } } } } // Do a 2nd round to discover resource replacements foreach (var comp in carcomps) { foreach (var res in comp.Children.CarResourceCollection) { foreach (var conn in res.SrcConnections.ReferenceSwapCollection) { if (conn.SrcEnds.CarResource != null) { var referenced = result.Where(r => r.FileName == res.Attributes.ResourcePath); var referer = result.Where(r => r.FileName == conn.SrcEnds.CarResource.Attributes.ResourcePath); if (referer.Any() && referenced.Any()) { referenced.First().References.Add(conn.Attributes.ReferenceName, referer.First()); } else { GMEConsole.Error.WriteLine("Internal error, resource was not found: " + conn.DstEnds.CarResource.Attributes.ResourcePath); } } } } } return result; }
private string PrepareResource(CarResource res) { string content = null; List <string> prelines = new List <string>(); List <string> paramlines = new List <string>(); List <string> postlines = new List <string>(); bool paramfound = false; bool paramfinished = false; int[] lastparamcolumn = null; using (StreamReader reader = new StreamReader(Path.Combine(ProjectDir, CarModelDir, res.FileName))) { while (!reader.EndOfStream) { string line = reader.ReadLine(); if (!paramfound) { prelines.Add(line); } else if (paramfinished) { postlines = new List <string>(); } else { string tmp = line.Trim(); if (tmp.StartsWith("{")) { lastparamcolumn = ProcessParamColumn(line); paramlines.Add(line); } else if (tmp.StartsWith("'")) { paramlines.Add(line); } else { paramfinished = true; postlines.Add(line); } } if (line == "[PARAMETER]") { paramfound = true; } } } StringBuilder b = new StringBuilder(); using (StringWriter writer = new StringWriter(b)) { foreach (string s in prelines) { writer.WriteLine(s); } foreach (string s in paramlines) { writer.WriteLine(ProcessParamLine(s, res.Parameters, lastparamcolumn)); } foreach (string s in postlines) { writer.WriteLine(s); } } return(b.ToString()); }
private List <CarResource> CollectCarResources(List <CyPhy.CarModel> carcomps) { List <CarResource> result = new List <CarResource>(); foreach (var comp in carcomps) { foreach (var res in comp.Children.CarResourceCollection) { // Is the resource already listed? CarResource currres = null; var existingres = result.Where(r => r.FileName == res.Attributes.ResourcePath); if (existingres.Any()) { if (existingres.Count() > 1) { throw new Exception("Internal error: car resource found more than once."); } currres = existingres.First(); } else { currres = new CarResource() { FileName = res.Attributes.ResourcePath }; result.Add(currres); } foreach (var param in res.SrcConnections.CarResourceParameterCollection) { if (param.SrcEnds.CarParameter != null) { if (param.SrcEnds.CarParameter.Attributes.Value.Length == 0) { GMEConsole.Warning.WriteLine("Empty parameter value for '" + param.SrcEnds.CarParameter.Name + "', ignoring."); } else { var existingparam = currres.Parameters.Where(p => p.Name == param.SrcEnds.CarParameter.Name); if (existingparam.Any()) { GMEConsole.Warning.WriteLine("Parameter '" + existingparam.First().Name + "' set twice on resource '" + currres.FileName + "'. Ignoring second setting."); } else { var newparam = new CarFileParam() { Name = param.SrcEnds.CarParameter.Name, Value = param.SrcEnds.CarParameter.Attributes.Value, Replacement = ((param.Attributes.ParameterKey.Length > 0) ? AdamsCarFileReplacement.FromString(param.Attributes.ParameterKey) : null) }; currres.Parameters.Add(newparam); } } } } } } // Do a 2nd round to discover resource replacements foreach (var comp in carcomps) { foreach (var res in comp.Children.CarResourceCollection) { foreach (var conn in res.SrcConnections.ReferenceSwapCollection) { if (conn.SrcEnds.CarResource != null) { var referenced = result.Where(r => r.FileName == res.Attributes.ResourcePath); var referer = result.Where(r => r.FileName == conn.SrcEnds.CarResource.Attributes.ResourcePath); if (referer.Any() && referenced.Any()) { referenced.First().References.Add(conn.Attributes.ReferenceName, referer.First()); } else { GMEConsole.Error.WriteLine("Internal error, resource was not found: " + conn.DstEnds.CarResource.Attributes.ResourcePath); } } } } } return(result); }