using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using RadicadeServer.DB; using RadicadeServer.Extensions; using RadicadeServer.RecNet.Enums.Inventions; using RadicadeServer.RecNet.Inventions; using RadicadeServer.Utils; namespace RadicadeServer.API.Inventions.V6 { [Route("api/inventions/v6")] public class InventionsV6(RecDBContext db) : Controller { [HttpPost("save"), Authorize] public async Task SaveInventionV6([FromBody] NewInventionRequestDTO_V6 dto) { var userId = User.GetUserId(); if (!dto.name.IsPure()) return new FullInvention { Status = InventionStatus.InnapropriateName }; if (!dto.description.IsPure()) return new FullInvention { Status = InventionStatus.InnapropriateDescription }; var playerUnlocks = await db.Unlocks.FirstOrDefaultAsync(x => x.AccountId == userId); if (playerUnlocks == null) return null; var invention = new InventionDB { Name = dto.name, CreatorPlayerId = (long)userId!, InstantiationCost = dto.instantiationCost, LightsCost = dto.lightsCost, RoomOrigin = dto.creationRoomId, ImageName = dto.imageName, BlobName = dto.inventionDataFilename, Description = dto.description, ReplicationId = Guid.NewGuid().ToString(), GeneralPermission = InventionRole.Publish }; await db.AddAsync(invention); await db.SaveChangesAsync(); var version = new InventionVersion { BlobName = dto.inventionDataFilename, ChipsCost = dto.chipsCost, CloudVariablesCost = dto.cloudVariablesCost, InstantiationCost = dto.instantiationCost, InventionId = invention.InventionId, ReplicationId = invention.ReplicationId, LightsCost = invention.LightsCost, VersionNumber = invention.CurrentVersionNumber }; await db.InventionVersions.AddAsync(version); var inventionIds = playerUnlocks.Inventions.FromJson>(); if (inventionIds == null) inventionIds = new List(); if (!inventionIds.Contains(invention.InventionId)) { inventionIds.Add(invention.InventionId); playerUnlocks.Inventions = inventionIds.ToJson(); await db.SaveChangesAsync(); } return await invention.ToFullInvention(db); } } }