public async Task <IActionResult> AddActivityGoal([FromRoute] Guid id, [FromBody] ActivityGoalForm form) { if (!ModelState.IsValid) { return(HttpResponseHelper.BadRequest(ModelState)); } var activity = await _context.Activities.Where(a => a.Id.Equals(id)).FirstOrDefaultAsync(); if (activity == null) { return(HttpResponseHelper.NotFound("No such Activity found.")); } var goal = new Goal { Description = form.Description, Concern = new ConcernMatrix { Coordinates = new Matrix { X = 0, Y = 0 } }, RewardResource = new RewardResourceMatrix { Coordinates = new Matrix { X = 0, Y = 0 } }, Feedback = new GoalFeedback() }; _context.Entry(activity).State = EntityState.Modified; if (activity.Goals?.Count > 0) { activity.Goals.Add(goal); } else { activity.Goals = new List <Goal> { goal }; } var error = await SaveChangesAsync(); if (error != null) { return(error); } return(Ok(activity)); }
public async Task AddActivityGoal() { var session = await Login(); var currentSeed = Guid.NewGuid(); using (var client = new HttpClient { BaseAddress = new Uri(ServerUrl) }) { client.AcceptJson().AddSessionHeader(session.Id); var form = new Activity { Name = $"Name.{currentSeed}", Description = $"Description.{currentSeed}" }; // Add Activity with valid data var activityResponse = await client.PostAsJsonAsync($"/api/activities", form); Assert.Equal(HttpStatusCode.Created, activityResponse.StatusCode); var activity = await activityResponse.Content.ReadAsJsonAsync <Activity>(); Assert.Equal($"Name.{currentSeed}", activity.Name); var goalForm = new ActivityGoalForm { Description = $"Description.{currentSeed}" }; activityResponse = await client.PutAsJsonAsync($"/api/activities/{activity.Id}/goal", goalForm); Assert.Equal(HttpStatusCode.OK, activityResponse.StatusCode); activity = await activityResponse.Content.ReadAsJsonAsync <Activity>(); Assert.Equal(1, activity.Goals.Count); } }