Skip to content

Commit

Permalink
Merge pull request #5 from Nuxify/refactor/api-nuxify-templates-impro…
Browse files Browse the repository at this point in the history
…vements

refactor: api templates improvements
  • Loading branch information
kabaluyot authored Nov 4, 2024
2 parents 78e9bc4 + b2007f7 commit fdcf126
Show file tree
Hide file tree
Showing 10 changed files with 202 additions and 254 deletions.
1 change: 1 addition & 0 deletions src/lib/core/application/service/store/social.api.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export interface StateDTO<T> {
}

export interface CreatePostRequest {
userId: number
title: string
body: string
}
Expand Down
225 changes: 28 additions & 197 deletions src/lib/core/application/service/store/social.api.store.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
import { writable } from 'svelte/store'
import { SocialRepository } from '$lib/servicecontainer'
import type { ErrorAPIResponse } from '$lib/core/domain/models/dto'
import type {
CreatePostResponse as ModelCreatePostResponse,
CommentResponse as ModelCommentResponse,
PostResponse as ModelPostResponse
} from '$lib/core/domain/models/social.dto'
import type {
StateDTO,
CreatePostRequest,
CreatePostResponse,
CommentResponse,
PostResponse
} from './social.api.dto'
import { createAPIHandler } from '$lib/internal/api.store'

const createPostState: StateDTO<CreatePostResponse> = {
const createPostWritable = writable<StateDTO<CreatePostResponse>>({
state: {
LOADING: false,
SUCCESS: false,
Expand All @@ -20,8 +25,8 @@ const createPostState: StateDTO<CreatePostResponse> = {
errorCode: null,
data: {} as CreatePostResponse
}
}
const getCommentsState: StateDTO<CommentResponse[]> = {
})
const getCommentsWritable = writable<StateDTO<CommentResponse[]>>({
state: {
LOADING: false,
SUCCESS: false,
Expand All @@ -32,8 +37,8 @@ const getCommentsState: StateDTO<CommentResponse[]> = {
errorCode: null,
data: [] as CommentResponse[]
}
}
const getPostsState: StateDTO<PostResponse[]> = {
})
const getPostsWritable = writable<StateDTO<PostResponse[]>>({
state: {
LOADING: false,
SUCCESS: false,
Expand All @@ -44,201 +49,27 @@ const getPostsState: StateDTO<PostResponse[]> = {
errorCode: null,
data: [] as PostResponse[]
}
}

const createPostWritable = writable(createPostState)
const getPostsWritable = writable(getPostsState)
const getCommentsWritable = writable(getCommentsState)

export const createPostStore = {
...createPostWritable,
async CreatePost(request: CreatePostRequest): Promise<void> {
// loading
createPostWritable.update((store) => {
store.state.LOADING = true
store.state.SUCCESS = false
store.state.FAILED = false
return store
})

try {
const response = await SocialRepository.CreatePost({
title: request.title,
body: request.body
})

// success
createPostWritable.update((store) => {
store.state.LOADING = false
store.state.SUCCESS = true
store.state.FAILED = false
store.response = {
message: 'Successfully created post.', // FIXME: this should be assigned from API response
errorCode: null,
data: {
id: response.id,
title: response.title,
body: response.body
}
}
return store
})
} catch (error) {
const err = error as ErrorAPIResponse

// failed
createPostWritable.update((store) => {
store.state.LOADING = false
store.state.SUCCESS = false
store.state.FAILED = true
store.response = {
message: 'Error occurred while creating post.', // FIXME: this should be assigned from API response
errorCode: err.errorCode ?? null, // FIXME: this should be assigned from API response
data: {} as CreatePostResponse
}
return store
})
} finally {
// reset
createPostWritable.update((store) => {
store.state.LOADING = false
store.state.SUCCESS = false
store.state.FAILED = false
return store
})
}
}
}

export const getCommentsStore = {
...getCommentsWritable,
async GetCommentsByPost(postId: number): Promise<void> {
// loading
getCommentsWritable.update((store) => {
store.state.LOADING = true
store.state.SUCCESS = false
store.state.FAILED = false
return store
})
})

try {
const response = await SocialRepository.GetCommentsByPostId(postId)
const comments = response.map((item) => {
const comment: CommentResponse = {
id: item.id,
postId: item.postId,
name: item.name,
email: item.email,
body: item.body
}

return comment
})

// success
getCommentsWritable.update((store) => {
store.state.LOADING = false
store.state.SUCCESS = true
store.state.FAILED = false
store.response = {
message: 'Successfully fetched comments.', // FIXME: this should be assigned from API response
errorCode: null,
data: comments
}
return store
})
} catch (error) {
const err = error as ErrorAPIResponse

// failed
getCommentsWritable.update((store) => {
store.state.LOADING = false
store.state.SUCCESS = false
store.state.FAILED = true
store.response = {
message: 'Error occurred while fetching comments.', // FIXME: this should be assigned from API response
errorCode: err.errorCode ?? null, // FIXME: this should be assigned from API response
data: []
}
return store
})
} finally {
// reset
getCommentsWritable.update((store) => {
store.state.LOADING = false
store.state.SUCCESS = false
store.state.FAILED = false
return store
})
}
}
}

export const getPostsStore = {
...getPostsWritable,
async GetPosts(): Promise<void> {
// loading
getPostsWritable.update((store) => {
store.state.LOADING = true
store.state.SUCCESS = false
store.state.FAILED = false
return store
})

try {
const response = await SocialRepository.GetPosts()
const posts = response.map((item) => {
const post: PostResponse = {
id: item.id,
userId: item.userId,
title: item.title,
body: item.body
}

return post
})
export const createPostStore = createAPIHandler<
CreatePostRequest,
ModelCreatePostResponse,
CreatePostResponse
>(createPostWritable, SocialRepository.CreatePost.bind(SocialRepository))

// success
getPostsWritable.update((store) => {
store.state.LOADING = false
store.state.SUCCESS = true
store.state.FAILED = false
store.response = {
message: 'Successfully fetched posts.', // FIXME: this should be assigned from API response
errorCode: null,
data: posts
}
return store
})
} catch (error) {
const err = error as ErrorAPIResponse
export const getPostsStore = createAPIHandler<void, ModelPostResponse[], PostResponse[]>(
getPostsWritable,
SocialRepository.GetPosts.bind(SocialRepository)
)

// failed
getPostsWritable.update((store) => {
store.state.LOADING = false
store.state.SUCCESS = false
store.state.FAILED = true
store.response = {
message: 'Error occurred while fetching posts.', // FIXME: this should be assigned from API response
errorCode: err.errorCode ?? null, // FIXME: this should be assigned from API response
data: []
}
return store
})
} finally {
// reset
getPostsWritable.update((store) => {
store.state.LOADING = false
store.state.SUCCESS = false
store.state.FAILED = false
return store
})
}
}
}
export const getPostCommentsStore = createAPIHandler<
number,
ModelCommentResponse[],
CommentResponse[]
>(getCommentsWritable, SocialRepository.GetPostComments.bind(SocialRepository))

export const socialAPI = {
createPostStore,
getCommentsStore,
getPostsStore
getPostsStore,
getPostCommentsStore
}
1 change: 1 addition & 0 deletions src/lib/core/domain/models/social.dto.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export interface CreatePostRequest {
userId: number
title: string
body: string
}
Expand Down
28 changes: 25 additions & 3 deletions src/lib/core/domain/repository/social.repository.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { APIResponse } from '$lib/core/domain/models/dto'
import type {
CreatePostRequest,
CreatePostResponse,
Expand All @@ -6,7 +7,28 @@ import type {
} from '$lib/core/domain/models/social.dto'

export interface ISocialRepository {
CreatePost(request: CreatePostRequest): Promise<CreatePostResponse>
GetPosts(): Promise<PostResponse[]>
GetCommentsByPostId(postId: number): Promise<CommentResponse[]>
/**
* Create a post
*
* @param {CreatePostRequest<APIResponse><CreatePostResponse>} request
*
* @return {Promise<APIResponse><CreatePostResponse>}
*/
CreatePost(request: CreatePostRequest): Promise<APIResponse<CreatePostResponse>>

/**
* Get posts
*
* @return {Promise<APIResponse><PostResponse>[]}
*/
GetPosts(): Promise<APIResponse<PostResponse[]>>

/**
* Get post comments
*
* @param {number<APIResponse><CommentResponse>[]} postId
*
* @return {Promise<APIResponse><CommentResponse>[]}
*/
GetPostComments(postId: number): Promise<APIResponse<CommentResponse[]>>
}
Loading

0 comments on commit fdcf126

Please sign in to comment.