public async Task <HttpResponseMessage> Post([FromBody] Card card) { var principal = HttpContext.Current.User as Principal; using (var repository = new Repository(principal.UserId)) { if (repository.ServiceUser == null) { return(Request.CreateResponse(HttpStatusCode.Forbidden)); } var cardModel = repository.CreateCard(card.Name, Guid.NewGuid()); await repository.Context.Entry(cardModel.Type).Reference(x => x.TemplateInfo).LoadAsync(); card = cardModel.FromDal(); card.Type = cardModel.Type.FromDal(true); var cardGenerator = new CardGenerator(card); await cardGenerator.Generate(); await cardGenerator.Generate(false); cardModel.PngCreationJobId = BackgroundJob.Schedule(() => CardGenerator.CreatePngJob(card.Guid, card.Faction.Name, card.Type.Name), TimeSpan.FromMinutes(1)); await repository.Context.SaveChangesAsync(); return(Request.CreateResponse(card)); } }
public async Task <IActionResult> Post([FromBody] Card card) { using (var repository = new Repository(HttpContext.GetUserGuid())) { if (!AuthorizeService.HashRight(repository.ServiceUser?.Role, Rights.CreateCard)) { return(Forbid()); } var cardModel = repository.CreateCard(card.Name, Guid.NewGuid()); var serieModel = await repository.Context.Series.FindByGuidAsync(card.Serie?.Guid); var factionModel = await repository.Context.Factions.FindByGuidAsync(card.Faction?.Guid); var cardTypeModel = await repository.Context.CardTypes.FindByGuidAsync(card.Type?.Guid); var statusModel = await repository.Context.Statuses.FindByGuidAsync(card.Status?.Guid); var ruleSetModel = await repository.Context.RuleSets.FindByGuidAsync(card.RuleSet?.Guid); var artworkLicenseModel = await repository.Context.Licenses.FindByGuidAsync(card.ArtworkLicense?.Guid); cardModel.Patch(card, serieModel, factionModel, cardTypeModel, statusModel, ruleSetModel, artworkLicenseModel, repository.ServiceUser); await repository.Context.SaveChangesAsync(); await repository.Context.Entry(cardTypeModel).Reference(x => x.TemplateInfo).LoadAsync(); card = cardModel.FromDal(); card.Type = cardModel.Type.FromDal(true); var cardGenerator = new CardGenerator(card); await cardGenerator.Generate(); await cardGenerator.Generate(false); cardModel.PngCreationJobId = BackgroundJob.Schedule(() => CardGenerator.CreatePngJob(card.Guid, card.Faction.Name, card.Type.Name), TimeSpan.FromMinutes(1)); await repository.Context.SaveChangesAsync(); return(Ok(card)); } }
public async Task <IActionResult> Patch(Guid id, [FromBody] Card card, bool?forceGeneration) { using (var repository = new Repository(HttpContext.GetUserGuid())) { if (!AuthorizeService.HashRight(repository.ServiceUser?.Role, Rights.EditCard)) { return(Forbid()); } var cardModel = await repository.Context.Cards.FindByGuidAsync(id); await repository.Context.Entry(cardModel).Reference(x => x.Status).LoadAsync(); var isFinal = cardModel.Status.Guid == PredefinedGuids.Final; if (isFinal && !AuthorizeService.HashRight(repository.ServiceUser?.Role, Rights.AllowCardStatusChange)) { return(Forbid("Card is marked as final")); } await repository.Context.Entry(cardModel).Reference(x => x.Creator).LoadAsync(); var isMyCard = repository.ServiceUser?.Guid == cardModel.Creator.Guid; if (!isMyCard && !AuthorizeService.HashRight(repository.ServiceUser?.Role, Rights.AllowOthersCardEdit)) { return(Forbid("This is not your card")); } await repository.Context.Entry(cardModel).Reference(x => x.Faction).LoadAsync(); await repository.Context.Entry(cardModel).Reference(x => x.Serie).LoadAsync(); await repository.Context.Entry(cardModel).Reference(x => x.Type).LoadAsync(); await repository.Context.Entry(cardModel).Reference(x => x.Status).LoadAsync(); await repository.Context.Entry(cardModel).Reference(x => x.ArtworkLicense).LoadAsync(); var serieModel = await repository.Context.Series.FindByGuidAsync(card.Serie.Guid); var factionModel = await repository.Context.Factions.FindByGuidAsync(card.Faction.Guid); var cardTypeModel = await repository.Context.CardTypes.FindByGuidAsync(card.Type.Guid); var statusModel = await repository.Context.Statuses.FindByGuidAsync(card.Status.Guid); var ruleSetModel = await repository.Context.RuleSets.FindByGuidAsync(card.RuleSet.Guid); var artworkLicenseModel = await repository.Context.Licenses.FindByGuidAsync(card.ArtworkLicense?.Guid); bool hasLayoutChanges = forceGeneration.HasValue && forceGeneration.Value; if (card.Name != cardModel.Name) { hasLayoutChanges = true; } if (card.Faction.Guid != cardModel.Faction.Guid) { hasLayoutChanges = true; } if (card.Type.Guid != cardModel.Type.Guid) { hasLayoutChanges = true; } if (card.FirstName != cardModel.FirstName) { hasLayoutChanges = true; } if (card.LastName != cardModel.LastName) { hasLayoutChanges = true; } if (card.Artist != cardModel.Artist) { hasLayoutChanges = true; } if (card.SubType != cardModel.SubType) { hasLayoutChanges = true; } if (card.Cost != cardModel.Cost) { hasLayoutChanges = true; } if (card.Loyalty != cardModel.Loyalty) { hasLayoutChanges = true; } if (card.Attack != cardModel.Attack) { hasLayoutChanges = true; } if (card.Defense != cardModel.Defense) { hasLayoutChanges = true; } if (card.Info != cardModel.Info) { hasLayoutChanges = true; } if (card.MarkdownText != cardModel.MarkdownText) { hasLayoutChanges = true; } cardModel.Patch(card, serieModel, factionModel, cardTypeModel, statusModel, ruleSetModel, artworkLicenseModel, repository.ServiceUser); await repository.Context.SaveChangesAsync(); await repository.Context.Entry(cardTypeModel).Reference(x => x.TemplateInfo).LoadAsync(); card = cardModel.FromDal(); card.Type = cardModel.Type.FromDal(true); await repository.Context.SaveChangesAsync(); if (hasLayoutChanges) { var jobId = cardModel.PngCreationJobId; if (!string.IsNullOrEmpty(jobId)) { BackgroundJob.Delete(jobId); } var cardGenerator = new CardGenerator(card); await cardGenerator.Generate(); await cardGenerator.Generate(false); cardModel.PngCreationJobId = BackgroundJob.Schedule(() => CardGenerator.CreatePngJob(card.Guid, card.Faction.Name, card.Type.Name), TimeSpan.FromMinutes(1)); } await repository.Context.SaveChangesAsync(); return(Ok(cardModel.FromDal())); } }
public async Task <HttpResponseMessage> Patch([FromUri] Guid id, [FromBody] Card card) { var principal = HttpContext.Current.User as Principal; using (var repository = new Repository(principal.UserId)) { if (repository.ServiceUser == null) { return(Request.CreateResponse(HttpStatusCode.Forbidden)); } var cardModel = await repository.Context.Cards.FindByGuidAsync(id); await repository.Context.Entry(cardModel).Reference(x => x.Status).LoadAsync(); await repository.Context.Entry(cardModel).Reference(x => x.Creator).LoadAsync(); if (repository.ServiceUser != null) { await repository.Context.Entry(repository.ServiceUser).Reference(x => x.Role).LoadAsync(); if (cardModel.Status.Guid == PredefinedGuids.Final) { if (repository.ServiceUser.Role.Guid == PredefinedGuids.Developer || repository.ServiceUser.Role.Guid != PredefinedGuids.Administrator || repository.ServiceUser.Role.Guid != PredefinedGuids.ServiceUser) { return(Request.CreateResponse(HttpStatusCode.Forbidden, "Card is marked as final")); } } } await repository.Context.Entry(cardModel).Reference(x => x.Faction).LoadAsync(); await repository.Context.Entry(cardModel).Reference(x => x.Serie).LoadAsync(); await repository.Context.Entry(cardModel).Reference(x => x.Type).LoadAsync(); await repository.Context.Entry(cardModel).Reference(x => x.Status).LoadAsync(); var serieModel = await repository.Context.Series.FindByGuidAsync(card.Serie.Guid); var factionModel = await repository.Context.Factions.FindByGuidAsync(card.Faction.Guid); var cardTypeModel = await repository.Context.CardTypes.FindByGuidAsync(card.Type.Guid); var statusModel = await repository.Context.Statuses.FindByGuidAsync(card.Status.Guid); var ruleSetModel = await repository.Context.RuleSets.FindByGuidAsync(card.RuleSet.Guid); var hasLayoutChanges = false; if (card.Name != cardModel.Name) { hasLayoutChanges = true; } if (card.Faction.Guid != cardModel.Faction.Guid) { hasLayoutChanges = true; } if (card.Type.Guid != cardModel.Type.Guid) { hasLayoutChanges = true; } if (card.FirstName != cardModel.FirstName) { hasLayoutChanges = true; } if (card.LastName != cardModel.LastName) { hasLayoutChanges = true; } if (card.Artist != cardModel.Artist) { hasLayoutChanges = true; } if (card.SubType != cardModel.SubType) { hasLayoutChanges = true; } if (card.Cost != cardModel.Cost) { hasLayoutChanges = true; } if (card.Loyalty != cardModel.Loyalty) { hasLayoutChanges = true; } if (card.Attack != cardModel.Attack) { hasLayoutChanges = true; } if (card.Defense != cardModel.Defense) { hasLayoutChanges = true; } if (card.Info != cardModel.Info) { hasLayoutChanges = true; } if (card.LayoutText != cardModel.LayoutText) { hasLayoutChanges = true; } cardModel.Patch(card, serieModel, factionModel, cardTypeModel, statusModel, ruleSetModel, repository.ServiceUser); if (hasLayoutChanges) { var jobId = cardModel.PngCreationJobId; if (!string.IsNullOrEmpty(jobId)) { BackgroundJob.Delete(jobId); } var cardGenerator = new CardGenerator(card); await cardGenerator.Generate(); await cardGenerator.Generate(false); cardModel.PngCreationJobId = BackgroundJob.Schedule(() => CardGenerator.CreatePngJob(card.Guid, card.Faction.Name, card.Type.Name), TimeSpan.FromMinutes(1)); } await repository.Context.SaveChangesAsync(); return(Request.CreateResponse(cardModel.FromDal())); } }