-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SOA-10] Enable post with attachments (#20)
* chore ποΈ (deps): add flydrive * styles π : created file-upload-preview component * feat πΈ (wip): put/get files from configured disk * feat πΈ (be): add attachment model * feat πΈ (be): add response interfaces * feat πΈ (be): adapted controllers and services to handle attachments * fix β (be): correction to migration null fields * styles π : prepare file uploads and display * feat πΈ (be): introduce other assets for upload * feat πΈ (be): introduce other assets for upload - correction * styles π : improve display for image gallery and user actions placement * styles π : improvements on post create/update * fix β (be): regenerate migration, and correct timestamp tmz * refactor β¨ : minor nits * test π§ͺ (fix): correction to minimal browser tests
- Loading branch information
1 parent
72e213f
commit bf45cff
Showing
29 changed files
with
2,024 additions
and
296 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,38 @@ | ||
import type { HttpContext } from '@adonisjs/core/http' | ||
import { inject } from '@adonisjs/core' | ||
import Post from '#models/post' | ||
import { PostResponse } from 'app/interfaces/post' | ||
import PostsService from '#services/posts_service' | ||
import { PostResponse } from 'app/interfaces/post' | ||
import { PageObject } from '@adonisjs/inertia/types' | ||
import { PaginatedResponse } from 'app/interfaces/pagination' | ||
import type { HttpContext } from '@adonisjs/core/http' | ||
|
||
@inject() | ||
export default class FeedController { | ||
constructor(private readonly postsService: PostsService) {} | ||
|
||
async index( | ||
ctx: HttpContext | ||
): Promise<string | PageObject<{ posts: PaginatedResponse<PostResponse> }>> { | ||
constructor( | ||
private readonly postsService: PostsService | ||
) { } | ||
async index(ctx: HttpContext): Promise<string | PageObject<{ posts: PaginatedResponse<PostResponse> }>> { | ||
const page = ctx.request.qs().page || 1 | ||
|
||
const posts = await Post.query() | ||
.orderBy('updated_at', 'desc') | ||
.preload('user') | ||
.paginate(page, 10) | ||
|
||
const data: PostResponse[] = [] | ||
|
||
const data: PostResponse[] = [] | ||
for (const post of posts) { | ||
const resource = await this.postsService.serialize(post) | ||
const resource = await this.postsService.serialize(post); | ||
data.push(resource) | ||
} | ||
|
||
const { meta } = posts.toJSON() | ||
const { meta } = posts.toJSON(); | ||
|
||
return ctx.inertia.render('feed', { | ||
posts: { | ||
data, | ||
meta, | ||
}, | ||
meta | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { UUID } from "crypto" | ||
|
||
export interface AttachmentMetadataJSON { | ||
filename: string; | ||
size: number; | ||
mimetype: string; | ||
extension: string; | ||
} | ||
|
||
export interface AttachmentResponse { | ||
id: UUID; | ||
link: string; | ||
metadata: AttachmentMetadataJSON; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
export interface BaseEntity { | ||
updatedAt: string | ||
createdAt: string | ||
} | ||
export interface BaseEntity { | ||
updatedAt: string | ||
createdAt: string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,16 @@ | ||
export interface MetaResponse { | ||
total: number | ||
perPage: number | ||
currentPage: number | ||
lastPage: number | ||
firstPage: number | ||
firstPageUrl: string | ||
lastPageUrl: string | ||
nextPageUrl: string | null | ||
previousPageUrl: string | null | ||
} | ||
|
||
export interface PaginatedResponse<T> { | ||
data: T[] | ||
meta: MetaResponse | ||
} | ||
export interface MetaResponse { | ||
total: number | ||
perPage: number | ||
currentPage: number | ||
lastPage: number | ||
firstPage: number | ||
firstPageUrl: string | ||
lastPageUrl: string | ||
nextPageUrl: string | null | ||
previousPageUrl: string | null | ||
} | ||
|
||
export interface PaginatedResponse<T> { | ||
data: T[] | ||
meta: MetaResponse | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,12 @@ | ||
import { ModelObject } from '@adonisjs/lucid/types/model' | ||
import { AccountRole } from '#models/user' | ||
import { BaseEntity } from 'app/interfaces/base-entity' | ||
import { UUID } from 'crypto' | ||
|
||
export interface UserResponse extends ModelObject {} // TODO: Complete me. | ||
export interface UserResponse extends BaseEntity { | ||
id: UUID, | ||
role: AccountRole, | ||
name: string | ||
surname: string | ||
username: string | ||
email: string, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { DateTime } from 'luxon' | ||
import { BaseModel, column } from '@adonisjs/lucid/orm' | ||
import type { UUID } from 'crypto'; | ||
import { AttachmentMetadataJSON } from 'app/interfaces/attachment'; | ||
|
||
export enum AttachmentProvider { | ||
S3 = "S3", | ||
} | ||
|
||
export enum AttachmentType { | ||
IMAGE = "Image", | ||
AUDIO = "Audio", | ||
DOCUMENT = "Document", | ||
VIDEO = "Video", | ||
} | ||
|
||
export enum AttachmentModel { | ||
USER = "User", | ||
POST = "Post", | ||
} | ||
|
||
export class MetadataJSON { | ||
// TODO: Could benefit in adding class-validator? | ||
declare filename: string; | ||
declare size: number; | ||
declare mimetype: string; | ||
declare extension: string; | ||
|
||
constructor(data: AttachmentMetadataJSON) { | ||
|
||
if ((!data.filename && typeof data.filename !== 'string') || | ||
(!data.size && typeof data.size !== 'number') || | ||
(!data.mimetype && typeof data.mimetype !== 'string') || | ||
(!data.extension && typeof data.extension !== 'string')) { | ||
throw new Error("Invalid file.") | ||
} | ||
|
||
Object.assign(this, data) | ||
} | ||
} | ||
|
||
export default class Attachment extends BaseModel { | ||
@column({ isPrimary: true }) | ||
declare id: UUID | ||
|
||
@column() | ||
declare type: AttachmentType | ||
|
||
@column() | ||
declare model: AttachmentModel | ||
|
||
@column() | ||
declare model_id: UUID | ||
|
||
@column() | ||
declare external_key: string | ||
|
||
@column() | ||
provider: AttachmentProvider = AttachmentProvider.S3 | ||
|
||
@column({}) | ||
declare metadata: MetadataJSON; | ||
|
||
@column.dateTime({ autoCreate: true }) | ||
declare createdAt: DateTime | ||
|
||
@column.dateTime({ autoCreate: true, autoUpdate: true }) | ||
declare updatedAt: DateTime | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.