コード例 #1
0
        public async Task <Results <ProjectVm> > SaveAsync(ProjectSaveData saveData)
        {
            var results = new Results <ProjectVm>();

            try
            {
                var userId = await _getCurrentUserProcessor.GetUserIdAsync();

                var user = await _userContext.DataSet
                           .Include(u => u.Projects)
                           .FirstAsync(u => u.Id == userId);

                var project = user
                              .Projects
                              .FirstOrDefault(p => p.Id == saveData.Id);

                if (saveData.Id == null)
                {
                    project = new Project()
                    {
                        UserId = userId
                    };
                    user.Projects.Add(project);
                }
                else if (project == null || project.UserId != userId)
                {
                    results.AddError("This project could not be found or you do not own this project");
                }

                if (results.HasError)
                {
                    return(results);
                }

                project.Name = saveData.Name;
                await _userContext.SaveChangesAsync();

                results.Data.Id   = project.Id;
                results.Data.Name = project.Name;
                results.Data.NumberOfReferences = project.ProjectReferences.Count();
            }
            catch (Exception e)
            {
                results.AddException(new Exception("Could not save this project", e));
            }

            return(results);
        }
コード例 #2
0
 public async Task <JsonResult> Save(ProjectSaveData saveData)
 {
     // Saves or inserts
     return(Json(await _saveProjectProcessor.SaveAsync(saveData)));
 }