I have the following method
public ServiceActionResult < Models.Idea > UpdateIdea(int ideaSpaceId, int ideaId, string currentUsername, CreateIdea updatedIdea) { var currentUser = GetUser(currentUsername); var space = _context.IdeaSpaces.Include("Ideas.Upvotes").FirstOrDefault(s => s.Id == ideaSpaceId); if (space == null) return new ServiceActionResult < Models.Idea > (null, ServiceActionStatus.NotFound); var idea = space.Ideas.FirstOrDefault(i => i.Id == ideaId); if (idea == null) return new ServiceActionResult < Models.Idea > (null, ServiceActionStatus.NotFound); var userIsAdmin = UserIsInRoleForSpace(currentUser.Email, space, AllowedUserRole.Admin); if (idea.CreatedBy == currentUser || userIsAdmin) { idea.ModifiedBy = currentUser; idea.ModifiedDate = DateTime.UtcNow; _context.Entry(idea).CurrentValues.SetValues(updatedIdea); _context.SaveChanges(); return new ServiceActionResult < Models.Idea > (Mapper.Map < Models.Idea > (idea), ServiceActionStatus.Updated); } return new ServiceActionResult < Models.Idea > (null, ServiceActionStatus.Error); }
And I can sortof smell something bad, but I am unsure of what it is.
Also, is this method breaking SRP? – since it both fetches data and modifies and saves?
The reason for me asking, is that I am having a really hard time unit testing this method partly because of coupling to DbContext.