Skip to content

Commit

Permalink
feat: get latest and single-endpoint push
Browse files Browse the repository at this point in the history
  • Loading branch information
kirinnee committed Oct 26, 2023
1 parent 71aaeee commit 0fb6ee1
Show file tree
Hide file tree
Showing 27 changed files with 621 additions and 56 deletions.
51 changes: 46 additions & 5 deletions App/Modules/Cyan/API/V1/Controllers/PluginController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,15 @@ public class PluginController : AtomiControllerBase
private readonly CreatePluginVersionReqValidator _createPluginVersionReqValidator;
private readonly UpdatePluginVersionReqValidator _updatePluginVersionReqValidator;
private readonly SearchPluginVersionQueryValidator _searchPluginVersionQueryValidator;
private readonly PushPluginReqValidator _pluginReqValidator;


public PluginController(IPluginService service,
CreatePluginReqValidator createPluginReqValidator, UpdatePluginReqValidator updatePluginReqValidator,
SearchPluginQueryValidator searchPluginQueryValidator,
CreatePluginVersionReqValidator createPluginVersionReqValidator,
UpdatePluginVersionReqValidator updatePluginVersionReqValidator,
SearchPluginVersionQueryValidator searchPluginVersionQueryValidator, IUserService userService)
SearchPluginVersionQueryValidator searchPluginVersionQueryValidator, IUserService userService, PushPluginReqValidator pluginReqValidator)
{
this._service = service;
this._createPluginReqValidator = createPluginReqValidator;
Expand All @@ -49,6 +50,7 @@ public PluginController(IPluginService service,
this._updatePluginVersionReqValidator = updatePluginVersionReqValidator;
this._searchPluginVersionQueryValidator = searchPluginVersionQueryValidator;
this._userService = userService;
this._pluginReqValidator = pluginReqValidator;
}

[HttpGet]
Expand Down Expand Up @@ -171,22 +173,31 @@ public async Task<ActionResult<IEnumerable<PluginVersionPrincipalResp>>> SearchV
}

[HttpGet("slug/{username}/{pluginName}/versions/{ver}")]
public async Task<ActionResult<PluginVersionPrincipalResp>> GetVersion(string username, string pluginName, ulong ver,
public async Task<ActionResult<PluginVersionResp>> GetVersion(string username, string pluginName, ulong ver,
bool bumpDownload)
{
var plugin = await this._service.GetVersion(username, pluginName, ver, bumpDownload)
.Then(x => x?.ToResp(), Errors.MapAll);
return this.ReturnNullableResult(plugin,
new EntityNotFound("Plugin not found", typeof(PluginVersionPrincipal), $"{username}/{pluginName}:{ver}"));
new EntityNotFound("Plugin not found", typeof(PluginVersion), $"{username}/{pluginName}:{ver}"));
}

[HttpGet("slug/{username}/{pluginName}/version/latest")]
public async Task<ActionResult<PluginVersionResp>> GetVersion(string username, string pluginName, bool bumpDownload)
{
var plugin = await this._service.GetVersion(username, pluginName, bumpDownload)
.Then(x => x?.ToResp(), Errors.MapAll);
return this.ReturnNullableResult(plugin,
new EntityNotFound("Plugin not found", typeof(PluginVersion), $"{username}/{pluginName}"));
}

[HttpGet("id/{userId}/{pluginId:guid}/versions/{ver}")]
public async Task<ActionResult<PluginVersionPrincipalResp>> GetVersion(string userId, Guid pluginId, ulong ver)
public async Task<ActionResult<PluginVersionResp>> GetVersion(string userId, Guid pluginId, ulong ver)
{
var plugin = await this._service.GetVersion(userId, pluginId, ver)
.Then(x => x?.ToResp(), Errors.MapAll);
return this.ReturnNullableResult(plugin,
new EntityNotFound("Plugin not found", typeof(PluginVersionPrincipal), $"{userId}/{pluginId}:{ver}"));
new EntityNotFound("Plugin not found", typeof(PluginVersion), $"{userId}/{pluginId}:{ver}"));
}

[Authorize, HttpPost("slug/{username}/{pluginName}/versions")]
Expand Down Expand Up @@ -256,4 +267,34 @@ public async Task<ActionResult<PluginVersionPrincipalResp>> UpdateVersion(string
new EntityNotFound("Plugin not found", typeof(PluginPrincipal), $"{userId}/{pluginId}"));
}

[Authorize, HttpPost("push/{username}")]
public async Task<ActionResult<PluginVersionPrincipalResp>> CreateVersion(string username, [FromBody] PushPluginReq req)
{
var sub = this.Sub();
var version = await this._userService
.GetByUsername(username)
.ThenAwait(x => Task.FromResult(x?.Principal.Id == sub), Errors.MapAll)
.ThenAwait(async x =>
{
if (x)
{
return await this._pluginReqValidator
.ValidateAsyncResult(req, "Invalid PushPluginReq")
.Then(push => push.ToDomain(), Errors.MapAll)
.ThenAwait(domain =>
{
var (record, metadata, vRecord, vProperty) = domain;
return this._service.Push(username, record, metadata, vRecord, vProperty);
})
.Then(c => c?.ToResp(), Errors.MapAll);
}

return new Unauthorized("You are not authorized to create a plugin for this user")
.ToException();
});

return this.ReturnNullableResult(version,
new EntityNotFound("Plugin not found", typeof(PluginPrincipal), $"{username}/{req.Name}"));
}

}
53 changes: 48 additions & 5 deletions App/Modules/Cyan/API/V1/Controllers/ProcessorController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,15 @@ public class ProcessorController : AtomiControllerBase
private readonly CreateProcessorVersionReqValidator _createProcessorVersionReqValidator;
private readonly UpdateProcessorVersionReqValidator _updateProcessorVersionReqValidator;
private readonly SearchProcessorVersionQueryValidator _searchProcessorVersionQueryValidator;
private readonly PushProcessorReqValidator _processorReqValidator;


public ProcessorController(IProcessorService service,
CreateProcessorReqValidator createProcessorReqValidator, UpdateProcessorReqValidator updateProcessorReqValidator,
SearchProcessorQueryValidator searchProcessorQueryValidator,
CreateProcessorVersionReqValidator createProcessorVersionReqValidator,
UpdateProcessorVersionReqValidator updateProcessorVersionReqValidator,
SearchProcessorVersionQueryValidator searchProcessorVersionQueryValidator, IUserService userService)
SearchProcessorVersionQueryValidator searchProcessorVersionQueryValidator, IUserService userService, PushProcessorReqValidator processorReqValidator)
{
this._service = service;
this._createProcessorReqValidator = createProcessorReqValidator;
Expand All @@ -49,6 +50,7 @@ public ProcessorController(IProcessorService service,
this._updateProcessorVersionReqValidator = updateProcessorVersionReqValidator;
this._searchProcessorVersionQueryValidator = searchProcessorVersionQueryValidator;
this._userService = userService;
this._processorReqValidator = processorReqValidator;
}

[HttpGet]
Expand Down Expand Up @@ -170,24 +172,35 @@ public async Task<ActionResult<IEnumerable<ProcessorVersionPrincipalResp>>> Sear
}

[HttpGet("slug/{username}/{processorName}/versions/{ver}")]
public async Task<ActionResult<ProcessorVersionPrincipalResp>> GetVersion(string username, string processorName, ulong ver,
public async Task<ActionResult<ProcessorVersionResp>> GetVersion(string username, string processorName, ulong ver,
bool bumpDownload)
{
var processor = await this._service.GetVersion(username, processorName, ver, bumpDownload)
.Then(x => x?.ToResp(), Errors.MapAll);
return this.ReturnNullableResult(processor,
new EntityNotFound("Processor not found", typeof(ProcessorVersionPrincipal), $"{username}/{processorName}:{ver}"));
new EntityNotFound("Processor not found", typeof(ProcessorVersion), $"{username}/{processorName}:{ver}"));
}

[HttpGet("slug/{username}/{processorName}/versions/latest")]
public async Task<ActionResult<ProcessorVersionResp>> GetVersion(string username, string processorName, bool bumpDownload)
{
var processor = await this._service.GetVersion(username, processorName, bumpDownload)
.Then(x => x?.ToResp(), Errors.MapAll);
return this.ReturnNullableResult(processor,
new EntityNotFound("Processor not found", typeof(ProcessorVersion), $"{username}/{processorName}"));
}

[HttpGet("id/{userId}/{processorId:guid}/versions/{ver}")]
public async Task<ActionResult<ProcessorVersionPrincipalResp>> GetVersion(string userId, Guid processorId, ulong ver)
public async Task<ActionResult<ProcessorVersionResp>> GetVersion(string userId, Guid processorId, ulong ver)
{
var processor = await this._service.GetVersion(userId, processorId, ver)
.Then(x => x?.ToResp(), Errors.MapAll);
return this.ReturnNullableResult(processor,
new EntityNotFound("Processor not found", typeof(ProcessorVersionPrincipal), $"{userId}/{processorId}:{ver}"));
new EntityNotFound("Processor not found", typeof(ProcessorVersion), $"{userId}/{processorId}:{ver}"));
}



[Authorize, HttpPost("slug/{username}/{processorName}/versions")]
public async Task<ActionResult<ProcessorVersionPrincipalResp>> CreateVersion(string username, string processorName,
[FromBody] CreateProcessorVersionReq req)
Expand Down Expand Up @@ -255,4 +268,34 @@ public async Task<ActionResult<ProcessorVersionPrincipalResp>> UpdateVersion(str
new EntityNotFound("Processor not found", typeof(ProcessorPrincipal), $"{userId}/{processorId}"));
}

[Authorize, HttpPost("push/{username}")]
public async Task<ActionResult<ProcessorVersionPrincipalResp>> CreateVersion(string username, [FromBody] PushProcessorReq req)
{
var sub = this.Sub();
var version = await this._userService
.GetByUsername(username)
.ThenAwait(x => Task.FromResult(x?.Principal.Id == sub), Errors.MapAll)
.ThenAwait(async x =>
{
if (x)
{
return await this._processorReqValidator
.ValidateAsyncResult(req, "Invalid PushProcessorReq")
.Then(push => push.ToDomain(), Errors.MapAll)
.ThenAwait(domain =>
{
var (record, metadata, vRecord, vProperty) = domain;
return this._service.Push(username, record, metadata, vRecord, vProperty);
})
.Then(c => c?.ToResp(), Errors.MapAll);
}

return new Unauthorized("You are not authorized to create a processor for this user")
.ToException();
});

return this.ReturnNullableResult(version,
new EntityNotFound("Processor not found", typeof(ProcessorPrincipal), $"{username}/{req.Name}"));
}

}
53 changes: 52 additions & 1 deletion App/Modules/Cyan/API/V1/Controllers/TemplateController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,24 @@ public class TemplateController : AtomiControllerBase
{
private readonly ITemplateService _service;
private readonly IUserService _userService;

private readonly ILogger<TemplateController> _logger;

private readonly CreateTemplateReqValidator _createTemplateReqValidator;
private readonly UpdateTemplateReqValidator _updateTemplateReqValidator;
private readonly SearchTemplateQueryValidator _searchTemplateQueryValidator;
private readonly CreateTemplateVersionReqValidator _createTemplateVersionReqValidator;
private readonly UpdateTemplateVersionReqValidator _updateTemplateVersionReqValidator;
private readonly SearchTemplateVersionQueryValidator _searchTemplateVersionQueryValidator;
private readonly PushTemplateReqValidator _templateReqValidator;


public TemplateController(ITemplateService service,
CreateTemplateReqValidator createTemplateReqValidator, UpdateTemplateReqValidator updateTemplateReqValidator,
SearchTemplateQueryValidator searchTemplateQueryValidator,
CreateTemplateVersionReqValidator createTemplateVersionReqValidator,
UpdateTemplateVersionReqValidator updateTemplateVersionReqValidator,
SearchTemplateVersionQueryValidator searchTemplateVersionQueryValidator, IUserService userService)
SearchTemplateVersionQueryValidator searchTemplateVersionQueryValidator, IUserService userService, PushTemplateReqValidator templateReqValidator, ILogger<TemplateController> logger)
{
this._service = service;
this._createTemplateReqValidator = createTemplateReqValidator;
Expand All @@ -49,6 +53,8 @@ public TemplateController(ITemplateService service,
this._updateTemplateVersionReqValidator = updateTemplateVersionReqValidator;
this._searchTemplateVersionQueryValidator = searchTemplateVersionQueryValidator;
this._userService = userService;
this._templateReqValidator = templateReqValidator;
this._logger = logger;
}

[HttpGet]
Expand Down Expand Up @@ -182,6 +188,15 @@ public async Task<ActionResult<TemplateVersionResp>> GetVersion(string username,
new EntityNotFound("Template not found", typeof(TemplateVersionResp), $"{username}/{templateName}:{ver}"));
}

[HttpGet("slug/{username}/{templateName}/versions/latest")]
public async Task<ActionResult<TemplateVersionResp>> GetVersion(string username, string templateName, bool bumpDownload)
{
var template = await this._service.GetVersion(username, templateName, bumpDownload)
.Then(x => x?.ToResp(), Errors.MapAll);
return this.ReturnNullableResult(template,
new EntityNotFound("Template not found", typeof(TemplateVersionResp), $"{username}/{templateName}"));
}

[HttpGet("id/{userId}/{templateId:guid}/versions/{ver}")]
public async Task<ActionResult<TemplateVersionResp>> GetVersion(string userId, Guid templateId, ulong ver)
{
Expand All @@ -191,6 +206,9 @@ public async Task<ActionResult<TemplateVersionResp>> GetVersion(string userId, G
new EntityNotFound("Template not found", typeof(TemplateVersionPrincipal), $"{userId}/{templateId}:{ver}"));
}




[Authorize, HttpPost("slug/{username}/{templateName}/versions")]
public async Task<ActionResult<TemplateVersionPrincipalResp>> CreateVersion(string username, string templateName,
[FromBody] CreateTemplateVersionReq req)
Expand Down Expand Up @@ -264,4 +282,37 @@ public async Task<ActionResult<TemplateVersionPrincipalResp>> UpdateVersion(stri
return this.ReturnNullableResult(version,
new EntityNotFound("Template not found", typeof(TemplatePrincipal), $"{userId}/{templateId}"));
}

[Authorize, HttpPost("push/{username}")]
public async Task<ActionResult<TemplateVersionPrincipalResp>> CreateVersion(string username, [FromBody] PushTemplateReq req)
{
this._logger.LogInformation("Version, Template: {Template}", req.ToJson());
var sub = this.Sub();
var version = await this._userService
.GetByUsername(username)
.ThenAwait(x => Task.FromResult(x?.Principal.Id == sub), Errors.MapAll)
.ThenAwait(async x =>
{
if (x)
{
return await this._templateReqValidator
.ValidateAsyncResult(req, "Invalid PushTemplateReq")
.Then(push => push.ToDomain(), Errors.MapAll)
.ThenAwait(domain =>
{
var (record, metadata, vRecord, vProperty) = domain;
return this._service.Push(username, record, metadata, vRecord, vProperty,
req.Processors.Select(p => p.ToDomain()),
req.Plugins.Select(p => p.ToDomain()));
})
.Then(c => c?.ToResp(), Errors.MapAll);
}

return new Unauthorized("You are not authorized to create a template for this user")
.ToException();
});

return this.ReturnNullableResult(version,
new EntityNotFound("Template not found", typeof(TemplatePrincipal), $"{username}/{req.Name}"));
}
}
27 changes: 20 additions & 7 deletions App/Modules/Cyan/API/V1/Mappers/PluginMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,23 @@ namespace App.Modules.Cyan.API.V1.Mappers;

public static class PluginMapper
{
public static (PluginRecord, PluginMetadata, PluginVersionRecord, PluginVersionProperty) ToDomain(
this PushPluginReq req) =>
(
new PluginRecord { Name = req.Name },
new PluginMetadata
{
Project = req.Project,
Source = req.Source,
Email = req.Email,
Tags = req.Tags,
Description = req.Description,
Readme = req.Readme
},
new PluginVersionRecord { Description = req.Description, },
new PluginVersionProperty { DockerReference = req.DockerReference, DockerSha = req.DockerSha, }
);

public static (PluginRecord, PluginMetadata) ToDomain(this CreatePluginReq req) =>
(new PluginRecord { Name = req.Name },
new PluginMetadata
Expand Down Expand Up @@ -56,15 +73,11 @@ public static (PluginVersionProperty, PluginVersionRecord) ToDomain(this CreateP
(new PluginVersionProperty { DockerReference = req.DockerReference, DockerSha = req.DockerSha },
new PluginVersionRecord { Description = req.Description });

public static PluginVersionRecord ToDomain(this UpdatePluginVersionReq req) => new() { Description = req.Description };
public static PluginVersionRecord ToDomain(this UpdatePluginVersionReq req) =>
new() { Description = req.Description };

public static PluginVersionSearch ToDomain(this SearchPluginVersionQuery query) =>
new()
{
Search = query.Search,
Limit = query.Limit ?? 20,
Skip = query.Skip ?? 0,
};
new() { Search = query.Search, Limit = query.Limit ?? 20, Skip = query.Skip ?? 0, };

public static PluginVersionPrincipalResp ToResp(this PluginVersionPrincipal principal) =>
new(principal.Id, principal.Version, principal.CreatedAt,
Expand Down
17 changes: 17 additions & 0 deletions App/Modules/Cyan/API/V1/Mappers/ProcessorMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,23 @@ namespace App.Modules.Cyan.API.V1.Mappers;

public static class ProcessorMapper
{
public static (ProcessorRecord, ProcessorMetadata, ProcessorVersionRecord, ProcessorVersionProperty) ToDomain(
this PushProcessorReq req) =>
(
new ProcessorRecord { Name = req.Name },
new ProcessorMetadata
{
Project = req.Project,
Source = req.Source,
Email = req.Email,
Tags = req.Tags,
Description = req.Description,
Readme = req.Readme
},
new ProcessorVersionRecord { Description = req.Description, },
new ProcessorVersionProperty { DockerReference = req.DockerReference, DockerSha = req.DockerSha, }
);

public static (ProcessorRecord, ProcessorMetadata) ToDomain(this CreateProcessorReq req) =>
(new ProcessorRecord { Name = req.Name },
new ProcessorMetadata
Expand Down
Loading

0 comments on commit 0fb6ee1

Please sign in to comment.