public async Task <ActionResult> Delete(int id) { try { var game = await _context.Games.Include(x => x.Categories).Include(x => x.Dlcs).Include(x => x.Screenshots).FirstOrDefaultAsync(x => x.Id == id); if (game == null) { return(Ok()); } _context.Games.Remove(game); await _context.SaveChangesAsync(); Filerectory.DeleteFile(game.BoxArtLocalPath); Filerectory.DeleteFile(game.CiaLocalPath); game.Dlcs.ForEach(x => { Filerectory.DeleteFile(x.LocalPath); }); game.Screenshots.ForEach(x => { Filerectory.DeleteFile(x.LocalPath); }); return(Ok()); } catch (Exception ex) { return(StatusCode(500, ex.ToInfo())); } }
public async Task <ActionResult <GameInfo> > Put(int id, [FromForm] GameRequest value) { try { var game = await _context.Games.FirstOrDefaultAsync(x => x.Id == id); if (game == null) { return(BadRequest(new ExceptionInfo("That game doesn't exist.", $"GameId: {id}"))); } var data = JsonConvert.DeserializeObject <GameInfo>(value.Json); var folder = data.Name; Path.GetInvalidFileNameChars().ToList().ForEach(x => { folder = folder.Replace(x, '-'); }); var boxArt = value.BoxArtFile; var cia = value.CiaFile; var directory = Path.Combine(Paths.Attachment, folder); Filerectory.CreateDirectory(directory); if (boxArt != null) { Filerectory.DeleteFile(game.BoxArtLocalPath); var path = Path.Combine(directory, boxArt.FileName); using (var stream = System.IO.File.Create(path)) { await boxArt.CopyToAsync(stream); } game.BoxArtLocalPath = path; } if (cia != null) { Filerectory.DeleteFile(game.CiaLocalPath); var path = Path.Combine(directory, cia.FileName); using (var stream = System.IO.File.Create(path)) { await cia.CopyToAsync(stream); } game.CiaLocalPath = path; } game.Developer = data.Developer; game.GameplayUrl = data.GameplayUrl; game.IsLegit = data.IsLegit; game.Name = data.Name; game.NumberOfPlayers = data.NumberOfPlayers; game.Publisher = data.Publisher; game.Region = data.Region; game.ReleaseDate = data.ReleaseDate; game.TagName = string.IsNullOrEmpty(data.TagName) ? data.Name : data.TagName; await _context.SaveChangesAsync(); return(game); } catch (Exception ex) { return(StatusCode(500, ex.ToInfo())); } }