Skip to content
This repository was archived by the owner on Feb 10, 2025. It is now read-only.

Commit f22cfea

Browse files
committed
feat(ConsoleImage): add hash and is_available fields
fix(Model): if the field is a boolean does not take in account the empty check
1 parent 48d3633 commit f22cfea

File tree

4 files changed

+95
-48
lines changed

4 files changed

+95
-48
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
use Phinx\Migration\AbstractMigration;
4+
5+
class AddIsAvailableAndHashColumnsToConsoleImagesTable extends AbstractMigration
6+
{
7+
public function change()
8+
{
9+
$this->table('console_images')
10+
->addColumn('is_available', 'boolean', ['default' => false])
11+
->addColumn('hash', 'string', ['null' => false])
12+
->update();
13+
}
14+
}

App/GraphQL/Query/ConsoleImage.php

+67-46
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,20 @@ public static function getMany()
1717
return [
1818
'type' => Type::listOf(Types::consoleImage()),
1919
'description' => 'Get many console image',
20-
'resolve' => fn () => \App\Models\ConsoleImage::all()
20+
'args' => [[
21+
'name' => 'all',
22+
'description' => 'If true, include unpublished images',
23+
'type' => Type::boolean(),
24+
'defaultValue' => false
25+
]],
26+
'resolve' => function (ContainerInterface $container, $args) {
27+
return \App\Models\ConsoleImage::all()
28+
->filter(fn($image) => $args['all'] || $image['is_available'])
29+
->map(function ($image) use ($container) {
30+
$image['url'] = $container->get('services')['os_endpoint'] . $image['path'];
31+
return $image;
32+
});
33+
}
2134
];
2235
}
2336

@@ -26,18 +39,17 @@ public static function getOne()
2639
return [
2740
'type' => Types::consoleImage(),
2841
'description' => 'Get a console image',
29-
'args' => [
30-
[
31-
'name' => 'id',
32-
'description' => 'The Id of the console image',
33-
'type' => Type::string()
34-
]
35-
],
36-
'resolve' => function ($_, $args) {
37-
$item = \App\Models\ConsoleImage::query()->find($args['id']);
38-
if ($item === NULL)
42+
'args' => [[
43+
'name' => 'id',
44+
'description' => 'The Id of the console image',
45+
'type' => Type::string()
46+
]],
47+
'resolve' => function (ContainerInterface $container, $args) {
48+
$image = \App\Models\ConsoleImage::query()->find($args['id']);
49+
$image['url'] = $container->get('services')['os_endpoint'] . $image['path'];
50+
if ($image === NULL)
3951
return new Exception("Unknown console image", 404);
40-
return $item;
52+
return $image;
4153
}
4254
];
4355
}
@@ -52,36 +64,40 @@ public static function store()
5264
'saved' => ['type' => Type::boolean()]
5365
]
5466
]),
55-
'args' => [
56-
[
57-
'name' => 'image',
58-
'description' => 'Image to store',
59-
'type' => Type::nonNull(new InputObjectType([
60-
'name' => 'ConsoleImageStoreInput',
61-
'fields' => [
62-
'console_version' => ['type' => Type::nonNull(Type::string())],
63-
'software_version' => ['type' => Type::nonNull(Type::string())],
64-
'description' => ['type' => Type::string()]
65-
]
66-
]))
67-
]
68-
],
67+
'args' => [[
68+
'name' => 'image',
69+
'description' => 'Image to store',
70+
'type' => Type::nonNull(new InputObjectType([
71+
'name' => 'ConsoleImageStoreInput',
72+
'fields' => [
73+
'console_version' => ['type' => Type::nonNull(Type::string())],
74+
'software_version' => ['type' => Type::nonNull(Type::string())],
75+
'size' => ['type' => Type::nonNull(Type::int())],
76+
'hash' => ['type' => Type::nonNull(Type::string())],
77+
'is_available' => ['type' => Type::boolean(), 'default_value' => false],
78+
'description' => ['type' => Type::string()]
79+
]
80+
]))
81+
]],
6982
'resolve' => function (ContainerInterface $container, $args) {
7083
if (!$container->get(Session::class)->isAdmin())
7184
return new Exception("Forbidden", 403);
7285

7386
$image = new \App\Models\ConsoleImage();
7487
$image['id'] = uniqid();
75-
$image->setAttributesFromGraphQL($args['image'], ['console_version', 'software_version', 'description']);
88+
$image->setAttributesFromGraphQL(
89+
$args['image'],
90+
['console_version', 'software_version', 'description', 'is_available', 'size', 'hash']
91+
);
7692

77-
$filter = array_filter($container->get('console-versions'), fn ($v) => $v['id'] === $image['console_version']);
93+
$filter = array_filter($container->get('console-versions'), fn($v) => $v['id'] === $image['console_version']);
7894
if (count($filter) !== 1)
7995
return new Exception("Unknown console version", 404);
8096

8197
$image->generateExtraFields();
8298
if (\App\Models\ConsoleImage::query()
83-
->where('version', '=', $image['version'])
84-
->count() !== 0)
99+
->where('version', '=', $image['version'])
100+
->count() !== 0)
85101
return new Exception("Version already exists", 400);
86102

87103
return [
@@ -97,31 +113,36 @@ public static function update()
97113
return [
98114
'type' => Type::boolean(),
99115
'description' => 'Update a console image',
100-
'args' => [
101-
[
102-
'name' => 'image',
103-
'description' => 'Console image to update',
104-
'type' => Type::nonNull(new InputObjectType([
105-
'name' => 'ConsoleImageUpdateInput',
106-
'fields' => [
107-
'id' => ['type' => Type::nonNull(Type::id())],
108-
'software_version' => ['type' => Type::string()],
109-
'description' => ['type' => Type::string()]
110-
]
111-
]))
112-
]
113-
],
116+
'args' => [[
117+
'name' => 'image',
118+
'description' => 'Console image to update',
119+
'type' => Type::nonNull(new InputObjectType([
120+
'name' => 'ConsoleImageUpdateInput',
121+
'fields' => [
122+
'id' => ['type' => Type::nonNull(Type::id())],
123+
'software_version' => ['type' => Type::string()],
124+
'description' => ['type' => Type::string()],
125+
'is_available' => ['type' => Type::boolean()],
126+
'size' => ['type' => Type::int()],
127+
'hash' => ['type' => Type::string()]
128+
]
129+
]))
130+
]],
114131
'resolve' => function (ContainerInterface $container, $args) {
115132
if (!$container->get(Session::class)->isAdmin())
116133
return new Exception("Forbidden", 403);
117134

135+
/* @var $image \App\Models\ConsoleImage */
118136
$image = \App\Models\ConsoleImage::query()
119137
->find($args['image']['id']);
120138

121139
if ($image === NULL)
122140
return new Exception("Unknown console image", 404);
123141

124-
$image->setAttributesFromGraphQL($args['image'], ['software_version', 'description']);
142+
$image = $image->setAttributesFromGraphQL(
143+
$args['image'],
144+
['software_version', 'description', 'is_available', 'size', 'hash']
145+
);
125146
$image->generateExtraFields();
126147

127148
if (\App\Models\ConsoleImage::query()
@@ -140,7 +161,7 @@ public static function destroy()
140161
return [
141162
'type' => Type::boolean(),
142163
'args' => [
143-
'id' => [ 'type' => Type::nonNull(Type::id()) ]
164+
'id' => ['type' => Type::nonNull(Type::id())]
144165
],
145166
'resolve' => function (ContainerInterface $container, $args) {
146167
if (!$container->get(Session::class)->isAdmin())

App/GraphQL/Type/ConsoleImage.php

+13-1
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,21 @@ public function __construct()
3737
'type' => Type::string(),
3838
'description' => 'The path at where the image is located on "https://os.retrobox.tech", begin with a slash and end with .img.zip'
3939
],
40+
'url' => [
41+
'type' => Type::string(),
42+
'description' => 'Full download url'
43+
],
4044
'size' => [
4145
'type' => Type::int(),
42-
'description' => 'The size of the zip file'
46+
'description' => 'The size of the zip file in megabytes'
47+
],
48+
'hash' => [
49+
'type' => Type::string(),
50+
'description' => 'A SHA-256 hash or checksum of the zip file'
51+
],
52+
'is_available' => [
53+
'type' => Type::boolean(),
54+
'description' => 'If the image is marked as available then the image can be used on the desktop app'
4355
],
4456
'created_at' => [
4557
'type' => Types::dateTime()

App/Models/Model.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class Model extends \Illuminate\Database\Eloquent\Model
77
public function setAttributesFromGraphQL($graphQLArgs, array $fields): self
88
{
99
foreach ($fields as $field){
10-
if (isset($graphQLArgs[$field]) && !empty($graphQLArgs[$field])){
10+
if (isset($graphQLArgs[$field]) && (!empty($graphQLArgs[$field]) || is_bool($graphQLArgs[$field]))){
1111
$this[$field] = $graphQLArgs[$field];
1212
}
1313
}

0 commit comments

Comments
 (0)