public async Task <IActionResult> Post([FromBody] MediaGroupModel model) { MediaGroupModel exist = await _context.MediaGroups.FirstOrDefaultAsync(x => x.NormalizedName == model.NormalizedName); if (model.NormalizedName == Constants.ThumbnailGroup) { return(BadRequest($"Normalized group name {model.NormalizedName} is restricted.")); } if (exist != null) { return(BadRequest($"Media group '{model.Name}' already exists.")); } await _context.MediaGroups.AddAsync(model); try { await _context.SaveChangesAsync(); return(Ok(model)); } catch (Exception ex) { return(BadRequest(ex.Message)); } }
public async Task <ActionResult> Delete(Guid id) { MediaGroupModel model = await _context.MediaGroups.FirstOrDefaultAsync(x => x.Id == id); if (model == null) { return(NotFound($"Media Group with id '${id}' not found.")); } List <KeyVersion> keys = new List <KeyVersion>(); List <MediaLinkModel> images = await _context.MediaLinks.Include(x => x.Group).Where(x => x.Group.Id == model.Id).ToListAsync(); foreach (MediaLinkModel image in images) { string imageKey = $"{model.NormalizedName}/{image.Name}"; keys.Add(new KeyVersion() { Key = imageKey }); keys.Add(new KeyVersion() { Key = $"{Constants.ThumbnailGroup}/{imageKey}" }); } DeleteObjectsRequest request = new DeleteObjectsRequest { BucketName = _config["aws:bucket"], Objects = keys }; try { if (keys.Count > 0) { await _s3Client.DeleteObjectsAsync(request); } _context.MediaLinks.RemoveRange(images); _context.MediaGroups.Remove(model); await _context.SaveChangesAsync(); return(Ok(model)); } catch (Exception ex) { return(BadRequest(ex.Message)); } }
public async Task <ActionResult <MediaLinkModel> > Post([FromForm] CreateMediaLinkViewModel model) { MediaLinkModel exists = await _context.MediaLinks.FirstOrDefaultAsync(x => x.Name == model.Name); MediaGroupModel group = await _context.MediaGroups.FirstOrDefaultAsync(x => x.Id == model.Group); if (exists != null) { return(BadRequest($"An image with name {model.Name} already exists.")); } using (MemoryStream thumbnail = CreateThumbnail(model.File)) using (MemoryStream memoryStream = new MemoryStream()) { model.File.CopyTo(memoryStream); try { await PutImageAsync(memoryStream, $"{group.NormalizedName}/{model.Name}"); await PutImageAsync(thumbnail, $"{Constants.ThumbnailGroup}/{group.NormalizedName}/{model.Name}"); } catch (Exception ex) { return(BadRequest(ex.Message)); } } MediaLinkModel image = new MediaLinkModel() { Name = model.Name, Group = group, Url = $"https://{_config["aws:bucket"]}.s3.amazonaws.com/{group.NormalizedName}/{model.Name}", Thumbnail = $"https://{_config["aws:bucket"]}.s3.amazonaws.com/{Constants.ThumbnailGroup}/{group.NormalizedName}/{model.Name}" }; try { await _context.MediaLinks.AddAsync(image); await _context.SaveChangesAsync(); return(Ok(image)); } catch (Exception ex) { return(BadRequest(ex.Message)); } }
public async Task <ActionResult> Put([FromODataUri] Guid id, [FromBody] MediaGroupModel model) { MediaGroupModel original = await _context.MediaGroups.AsNoTracking().FirstOrDefaultAsync(x => x.Id == model.Id); if (model.NormalizedName == Constants.ThumbnailGroup) { return(BadRequest($"Normalized group name {model.NormalizedName} is restricted.")); } if (original == null) { return(NotFound($"Media Group with Name '{model.Name}' not found.")); } List <MediaLinkModel> images = await _context.MediaLinks.Include(x => x.Group).Where(x => x.Group.Id == original.Id).ToListAsync(); if (images.Count > 0) { return(BadRequest($"Unable to edit a group with existing images.")); } try { MediaGroupModel exists = await _context.MediaGroups.FirstOrDefaultAsync(x => x.NormalizedName == model.NormalizedName && x.Id != model.Id); if (exists != null) { return(BadRequest($"Media Group with Name {model.Name} already exists.")); } _context.MediaGroups.Update(model); await _context.SaveChangesAsync(); return(Ok(model)); } catch (Exception ex) { return(BadRequest(ex.Message)); } }