public async Task<Project> AddNew( Project project, ICollection<User> collaborators, IEnumerable<Tag> tags, IEnumerable<ProcessedImage> processedImages, string mainImage, IEnumerable<File> downloadableFiles) { collaborators.ForEach(c => project.Collaborators.Add(c)); tags.ForEach(t => project.Tags.Add(t)); processedImages.Select(ProcessedImage.ToImage).ForEach(image => { image = this.images.Attach(image); project.Images.Add(image); }); project.MainImageId = this.GetMainImageId(project, mainImage); downloadableFiles.ForEach(file => { file = this.files.Attach(file); project.Files.Add(file); }); this.projects.Add(project); await this.projects.SaveChangesAsync(); return project; }
private int GetMainImageId(Project project, string mainImage) { var id = project .Images .Where(pi => pi.OriginalFileName == mainImage) .Select(i => i.Id) .FirstOrDefault(); if (id == 0) { id = project.Images.Select(i => i.Id).First(); } return id; }
public async Task Edit( Project project, IEnumerable<User> newCollaborators, IEnumerable<User> deletedCollaborators, IEnumerable<Tag> requiredTags, IEnumerable<Tag> newUserTags, IEnumerable<Tag> deletedUserTags, IEnumerable<Image> updatedImages, string updatedMainImageUrl) { deletedCollaborators.ForEach(c => project.Collaborators.Remove(c)); newCollaborators.ForEach(c => { if (!project.Collaborators.Contains(c)) { project.Collaborators.Add(c); } }); project .Tags .Where(t => t.Type != TagType.UserSubmitted) .ToList() .ForEach(t => project.Tags.Remove(t)); requiredTags.ForEach(t => project.Tags.Add(t)); deletedUserTags.ForEach(t => project.Tags.Remove(t)); newUserTags.ForEach(t => { if (!project.Tags.Contains(t)) { project.Tags.Add(t); } }); project .Images .ToList() .ForEach(i => project.Images.Remove(i)); updatedImages.ForEach(i => project.Images.Add(i)); project.MainImage = updatedImages.FirstOrDefault(i => i.UrlPath == updatedMainImageUrl); this.projects.Update(project); await this.projects.SaveChangesAsync(); }