Skip to content

Commit

Permalink
Fix save logic for Resources and Volunteer tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
dpaquette committed Mar 7, 2018
1 parent 6e31213 commit b9f2daa
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Threading.Tasks;
using System.Threading.Tasks;
using AllReady.Extensions;
using AllReady.Models;
using MediatR;
Expand All @@ -16,14 +16,13 @@ public CreateOrEditResourceCommandHandler(AllReadyContext context)
}
public async Task<int> Handle(CreateOrEditResourceCommand message)
{
var resource = await GetResource(message) ?? new AllReady.Models.Resource();
var resource = await GetResource(message) ?? _context.Add(new AllReady.Models.Resource()).Entity;

resource.Name = message.Resource.Name;
resource.Description = message.Resource.Description;
resource.CampaignId = message.Resource.CampaignId;
resource.ResourceUrl = message.Resource.ResourceUrl;

_context.AddOrUpdate(resource);
await _context.SaveChangesAsync();

return resource.Id;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Linq;
using System.Linq;
using System.Threading.Tasks;
using AllReady.Models;
using MediatR;
Expand All @@ -22,7 +22,7 @@ public EditVolunteerTaskCommandHandler(AllReadyContext context, IAttachmentServi

public async Task<int> Handle(EditVolunteerTaskCommand message)
{
var volunteerTask = await _context.VolunteerTasks.Include(t => t.RequiredSkills).SingleOrDefaultAsync(t => t.Id == message.VolunteerTask.Id) ?? new VolunteerTask();
var volunteerTask = await _context.VolunteerTasks.Include(t => t.RequiredSkills).SingleOrDefaultAsync(t => t.Id == message.VolunteerTask.Id) ?? _context.Add(new VolunteerTask()).Entity;

volunteerTask.Name = message.VolunteerTask.Name;
volunteerTask.Description = message.VolunteerTask.Description;
Expand All @@ -47,8 +47,6 @@ public async Task<int> Handle(EditVolunteerTaskCommand message)
volunteerTask.RequiredSkills.AddRange(message.VolunteerTask.RequiredSkills.Where(mt => volunteerTask.RequiredSkills.All(ts => ts.SkillId != mt.SkillId)));
}

_context.AddOrUpdate(volunteerTask);

// Delete existing attachments
if (message.VolunteerTask.DeleteAttachments.Count > 0)
{
Expand Down Expand Up @@ -77,4 +75,4 @@ public async Task<int> Handle(EditVolunteerTaskCommand message)
return volunteerTask.Id;
}
}
}
}

0 comments on commit b9f2daa

Please sign in to comment.