public async Task <IActionResult> PutAnalyzedArray(int id, AnalyzedArrays analyzedArray) { if (id != analyzedArray.Id) { return(BadRequest("Id does not exist.")); } analyzedArray = ProcessArray(analyzedArray); _context.Entry(analyzedArray).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException e) { if (!AnalyzedArraysExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
/** * Process array method processes the array to display optimal path, * reachable boolean field to false(0) or true(1). */ public AnalyzedArrays ProcessArray(AnalyzedArrays analyzedArray) { //converts "ArrayComposition" field, which is in a form of e.g. 1,3,2,5 to a List of integers. List <int> stringToList = new List <int>(analyzedArray.ArrayComposition.Split(",").Select(x => int.Parse(x))); //converting the List to number array to be passed for processing. int[] numArray = stringToList.ToArray(); string optimalPath = ""; if (IsReachable(numArray)) { foreach (var optimalJump in FindPath(numArray)) { optimalPath += optimalJump.Value + " -> "; } optimalPath = optimalPath.Remove(optimalPath.LastIndexOf("-"), 2); analyzedArray.IsReachable = 1; analyzedArray.OptimalPath = optimalPath.Trim(); } else { analyzedArray.IsReachable = 0; analyzedArray.OptimalPath = "n/a"; } return(analyzedArray); }
public async Task <ActionResult <AnalyzedArrays> > PostAnalyzedArrays(AnalyzedArrays analyzedArray) { if (_context.AnalyzedArrays.Any(arr => arr.ArrayComposition.Equals(analyzedArray.ArrayComposition))) { return(Content("Array already exists.")); } analyzedArray = ProcessArray(analyzedArray); _context.AnalyzedArrays.Add(analyzedArray); await _context.SaveChangesAsync(); return(CreatedAtAction("GetAnalyzedArrays", new { id = analyzedArray.Id }, analyzedArray)); }