public async Task <ActionResult <Patch> > GetPatch(int id) { Data.Models.Patch patch = await context.Patches .Include(x => x.AppUser) .Include(x => x.NufUser) .Include(x => x.Tags) .Include(x => x.Ratings) .Include(x => x.Comments) .Include(x => x.Children) .ThenInclude(x => x.PatchFiles) .ThenInclude(pf => pf.File) .Include(x => x.Children) .ThenInclude(x => x.NufUser) .Include(x => x.Parent) .ThenInclude(x => x.PatchFiles) .ThenInclude(pf => pf.File) .Include(x => x.Parent) .ThenInclude(x => x.NufUser) .Include(x => x.PatchFiles) .ThenInclude(pf => pf.File) .FirstOrDefaultAsync(x => x.Id == id); if (patch == null) { return(NotFound()); } var model = mapper.Map <Patch>(patch); return(model); }
public async Task <ActionResult <Patch> > Delete([FromRoute] int fileId, [FromRoute] int id) { Data.Models.Patch existingPatch = await context.Patches .Include(x => x.NufUser) .Include(x => x.Tags) .Include(x => x.Ratings) .Include(x => x.Comments) .Include(x => x.Children) .ThenInclude(x => x.PatchFiles) .ThenInclude(pf => pf.File) .Include(x => x.Children) .ThenInclude(x => x.NufUser) .Include(x => x.Parent) .ThenInclude(x => x.PatchFiles) .ThenInclude(pf => pf.File) .Include(x => x.Parent) .ThenInclude(x => x.NufUser) .Include(x => x.PatchFiles) .ThenInclude(pf => pf.File) .FirstOrDefaultAsync(x => x.Id == id); var patchFile = existingPatch.PatchFiles.SingleOrDefault(x => x.FileId == fileId); existingPatch.PatchFiles.Remove(patchFile); await context.SaveChangesAsync(); cache.Remove(Constants.PatchCacheKey); cache.Remove(Constants.FileCacheKey); var model = mapper.Map <Patch>(existingPatch); return(model); }
private void UpdateTags(int id, Patch patch, Data.Models.Patch existingPatch) { List <Tag> tagsToRemove = existingPatch.Tags.Where(tag => !patch.Tags.Any(x => x.Name == tag.Name && x.PatchId == id)).ToList(); List <Tag> tagsToAdd = ( from tag in patch.Tags where !existingPatch.Tags.Any(x => x.Name == tag.Name && x.PatchId == id) select new Tag { PatchId = id, Name = tag.Name }).ToList(); context.Tags.AddRange(tagsToAdd); context.Tags.RemoveRange(tagsToRemove); }
public async Task <IActionResult> PutPatch([FromRoute] int id, [FromBody] Patch patch) { if (!PatchExists(id)) { return(NotFound()); } patch.DateUpdated = DateTime.UtcNow; Data.Models.Patch existingPatch = context.Patches.Where(x => x.Id == id).Include(x => x.Tags).Single(); context.Entry(existingPatch).CurrentValues.SetValues(patch); UpdateTags(id, patch, existingPatch); await context.SaveChangesAsync(); cache.Remove(Constants.PatchCacheKey); return(NoContent()); }
public async Task <ActionResult <Patch> > Post([FromBody] PatchFile patchFile) { try { Data.Models.Patch existingPatch = await context.Patches .Include(x => x.Tags) .Include(x => x.Ratings) .Include(x => x.AppUser) .Include(x => x.NufUser) .Include(x => x.PatchFiles) .ThenInclude(pf => pf.File) .OrderByDescending(x => x.DateCreated) .SingleOrDefaultAsync(x => x.Id == patchFile.PatchId); existingPatch.PatchFiles.Add(new Data.Models.PatchFile { FileId = patchFile.FileId, PatchId = patchFile.PatchId }); await context.SaveChangesAsync(); cache.Remove(Constants.PatchCacheKey); cache.Remove(Constants.FileCacheKey); Data.Models.Patch updatedPatch = await context.Patches .Include(x => x.Tags) .Include(x => x.Ratings) .Include(x => x.AppUser) .Include(x => x.NufUser) .Include(x => x.PatchFiles) .ThenInclude(pf => pf.File) .OrderByDescending(x => x.DateCreated) .SingleOrDefaultAsync(x => x.Id == patchFile.PatchId); var model = mapper.Map <Patch>(updatedPatch); return(CreatedAtAction("Post", model)); } catch (Exception e) { logger.LogError(e, "An error occurred creating the DB connection."); return(BadRequest()); } }