public async Task <TrackableMethodViewModel> SetMethodToTrack(TrackableMethodViewModel method) { var trackedMethods = await GetTrackedMethods(); if (trackedMethods.Count >= 5 && method.Selected) { throw new InvalidOperationException("Already selected 5 methods"); } using (FlashTunaDbContext db = new FlashTunaDbContext()) { if (await db.TrackedMethods .AnyAsync(x => x.Name == method.MethodName && x.ClassName == method.ClassName) && method.Selected) { throw new InvalidOperationException($"Method {method.MethodName} already tracked by System."); } if ((!await db.TrackedMethods .AnyAsync(x => x.Name == method.MethodName && x.ClassName == method.ClassName)) && (!method.Selected)) { throw new InvalidOperationException($"Method {method.MethodName} not tracked by System yet."); } if (method.Selected) { await db.TrackedMethods.AddAsync(new TrackedMethod() { ClassName = method.ClassName, Name = method.MethodName }); await db.SaveChangesAsync(); } else { var trackedMethod = await db.TrackedMethods.FirstAsync(x => x.ClassName == method.ClassName && x.Name == method.MethodName); db.TrackedMethods.Remove(trackedMethod); await db.SaveChangesAsync(); } return(new TrackableMethodViewModel() { ClassName = method.ClassName, MethodName = method.MethodName, Selected = method.Selected }); } }
public async Task <IActionResult> SetTrackableMethod([FromBody] TrackableMethodViewModel method) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } TrackableMethodViewModel updatedMethod = null; try { updatedMethod = await FlashTunaAnalyzer.Results.SetMethodToTrack(method); } catch (InvalidOperationException ex) { BadRequest(ex.Message); } return(Ok(updatedMethod)); }