Updated api responses for files and books data.
This commit is contained in:
@@ -15,14 +15,14 @@
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr v-for="file in files">
|
||||
<td>{{ file.fileName }}
|
||||
<td>{{ file.file_name }}
|
||||
<p class="mb-0" v-if="file.book">
|
||||
<a class="text-decoration-none" :href="'/book/'+file.book.id">{{
|
||||
file.book.title
|
||||
}}</a> <a href="" class="text-decoration-none link-secondary">{{ file.book.author }}</a>
|
||||
</p>
|
||||
</td>
|
||||
<td class="text-end">{{ formatSize(file.fileSize) }}</td>
|
||||
<td class="text-end">{{ file.file_size_human }}</td>
|
||||
<td>{{ file.extension }}</td>
|
||||
<td><a :href="'/file/'+ file.id" class="link-secondary">download</a></td>
|
||||
<td><a :href="'/file/delete/'+file.id" @click.prevent="deleteFile(file.id)" class="link-danger">remove</a>
|
||||
@@ -61,13 +61,6 @@ export default {
|
||||
deleteFile: function (fileId) {
|
||||
axios.get(window.location.origin + '/file/delete/' + fileId).then(() => this.getFiles())
|
||||
},
|
||||
formatSize: function (bytes) {
|
||||
if (bytes === 0) {
|
||||
return "0.00 B";
|
||||
}
|
||||
const e = Math.floor(Math.log(bytes) / Math.log(1024));
|
||||
return (bytes / Math.pow(1024, e)).toFixed(2) + ' ' + ' KMGTP'.charAt(e) + 'B';
|
||||
},
|
||||
getFiles: function () {
|
||||
axios.get(this.getFilesEndpoint(), {
|
||||
headers: {
|
||||
|
||||
@@ -2,23 +2,30 @@
|
||||
|
||||
namespace App\Entity;
|
||||
|
||||
use ApiPlatform\Core\Annotation\ApiFilter;
|
||||
use ApiPlatform\Core\Annotation\ApiResource;
|
||||
use ApiPlatform\Core\Annotation\ApiSubresource;
|
||||
use ApiPlatform\Core\Bridge\Doctrine\Common\Filter\SearchFilterInterface;
|
||||
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
|
||||
use App\Repository\BookRepository;
|
||||
use Doctrine\Common\Collections\ArrayCollection;
|
||||
use Doctrine\Common\Collections\Collection;
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
use Symfony\Component\Serializer\Annotation\Groups;
|
||||
|
||||
#[ApiResource]
|
||||
#[ORM\Entity(repositoryClass: BookRepository::class)]
|
||||
#[ApiFilter(SearchFilter::class, properties: ['title' => SearchFilterInterface::STRATEGY_PARTIAL])]
|
||||
class Book
|
||||
{
|
||||
#[ORM\Id]
|
||||
#[ORM\GeneratedValue]
|
||||
#[ORM\Column(type: 'integer')]
|
||||
#[Groups('file:read')]
|
||||
private $id;
|
||||
|
||||
#[ORM\Column(type: 'string', length: 255)]
|
||||
#[Groups('file:read')]
|
||||
private $title;
|
||||
|
||||
#[ORM\Column(type: 'string', length: 255)]
|
||||
@@ -50,6 +57,7 @@ class Book
|
||||
private $volume;
|
||||
|
||||
#[ORM\Column(type: 'string', length: 255)]
|
||||
#[Groups('file:read')]
|
||||
private $author;
|
||||
|
||||
#[ORM\Column(type: 'text', nullable: true)]
|
||||
|
||||
@@ -5,23 +5,28 @@ namespace App\Entity;
|
||||
use ApiPlatform\Core\Annotation\ApiResource;
|
||||
use App\Repository\FileRepository;
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
use Symfony\Component\Serializer\Annotation\Groups;
|
||||
use Symfony\Component\Serializer\Annotation\SerializedName;
|
||||
|
||||
#[ApiResource]
|
||||
#[ApiResource(collectionOperations: ['get'], itemOperations: ['get'], denormalizationContext: ['groups' => ['file:write']], normalizationContext: ['groups' => ['file:read']])]
|
||||
#[ORM\Entity(repositoryClass: FileRepository::class)]
|
||||
class File
|
||||
{
|
||||
#[ORM\Id]
|
||||
#[ORM\GeneratedValue]
|
||||
#[ORM\Column(type: 'integer')]
|
||||
#[Groups('file:read')]
|
||||
private $id;
|
||||
|
||||
#[ORM\Column(type: 'string', length: 255)]
|
||||
#[Groups('file:read')]
|
||||
private $file_name;
|
||||
|
||||
#[ORM\Column(type: 'integer')]
|
||||
private $file_size;
|
||||
|
||||
#[ORM\Column(type: 'string', length: 255)]
|
||||
#[Groups('file:read')]
|
||||
private $extension;
|
||||
|
||||
#[ORM\Column(type: 'string', length: 255)]
|
||||
@@ -29,6 +34,7 @@ class File
|
||||
|
||||
#[ORM\ManyToOne(targetEntity: Book::class, inversedBy: 'files')]
|
||||
#[ORM\JoinColumn(nullable: false)]
|
||||
#[Groups('file:read')]
|
||||
private $book;
|
||||
|
||||
public function getId(): ?int
|
||||
@@ -53,6 +59,15 @@ class File
|
||||
return $this->file_size;
|
||||
}
|
||||
|
||||
#[Groups('file:read')]
|
||||
#[SerializedName('file_size_human')]
|
||||
public function getFileSizeHuman(): string
|
||||
{
|
||||
$base = log($this->file_size) / log(1024);
|
||||
$suffix = array("B", "KB", "MB", "GB", "TB")[floor($base)];
|
||||
return round(pow(1024, $base - floor($base)), 2) . $suffix;
|
||||
}
|
||||
|
||||
public function setFileSize(int $file_size): self
|
||||
{
|
||||
$this->file_size = $file_size;
|
||||
|
||||
Reference in New Issue
Block a user