| 65 | } |
| 66 | |
| 67 | [ApiController, IsAuthenticated] |
| 68 | [ResponseCache(NoStore = true, Location = ResponseCacheLocation.None)] |
| 69 | public class AppsController : Controller |
| 70 | { |
| 71 | private readonly IDbContext _db; |
| 72 | private readonly EnvSettings _env; |
| 73 | private readonly IBlobService _blobService; |
| 74 | |
| 75 | public AppsController(IDbContext db, EnvSettings env, IBlobService blobService) |
| 76 | { |
| 77 | _db = db ?? throw new ArgumentNullException(nameof(db)); |
| 78 | _env = env ?? throw new ArgumentNullException(nameof(env)); |
| 79 | _blobService = blobService ?? throw new ArgumentNullException(nameof(blobService)); |
| 80 | } |
| 81 | |
| 82 | [HttpGet("/api/_apps")] |
| 83 | public async Task<IActionResult> ListApps() |
| 84 | { |
| 85 | var user = this.GetCurrentUserIdentity(); |
| 86 | |
| 87 | var apps = await _db.Connection.QueryAsync<Application>( |
| 88 | @"SELECT a.id, a.name, a.icon_path, a.app_key, |
| 89 | a.owner_id = @userId AS has_ownership, a.has_events, |
| 90 | u.lock_reason |
| 91 | FROM apps a |
| 92 | LEFT JOIN app_shares s |
| 93 | ON s.app_id = a.id |
| 94 | INNER JOIN users u |
| 95 | ON u.id = a.owner_id |
| 96 | WHERE (a.owner_id = @userId OR s.email = @userEmail) |
| 97 | AND a.deleted_at IS NULL |
| 98 | GROUP BY a.id, a.name, a.icon_path, a.app_key, u.lock_reason |
| 99 | ORDER by a.name", new { userId = user.Id, userEmail = user.Email }); |
| 100 | |
| 101 | return Ok(apps); |
| 102 | } |
| 103 | |
| 104 | [HttpPost("/api/_apps")] |
| 105 | public async Task<IActionResult> Create([FromBody] CreateAppRequestBody body) |
| 106 | { |
| 107 | var user = this.GetCurrentUserIdentity(); |
| 108 | var app = new Application |
| 109 | { |
| 110 | Id = NanoId.New(), |
| 111 | Name = body.Name, |
| 112 | AppKey = $"A-{_env.Region}-{NanoId.Numbers(10)}" |
| 113 | }; |
| 114 | |
| 115 | await _db.Connection.ExecuteScalarAsync<string>(@" |
| 116 | INSERT INTO apps (id, owner_id, name, app_key, has_events) |
| 117 | VALUES (@appId, @ownerId, @name, @appKey, false)", |
| 118 | new |
| 119 | { |
| 120 | appId = app.Id, |
| 121 | ownerId = user.Id, |
| 122 | name = app.Name, |
| 123 | appKey = app.AppKey |
| 124 | }); |
nothing calls this directly
no outgoing calls
no test coverage detected