Skip to content

Commit

Permalink
Update backend
Browse files Browse the repository at this point in the history
  • Loading branch information
dungngminh committed May 1, 2024
1 parent 8638c74 commit 1901553
Show file tree
Hide file tree
Showing 32 changed files with 382 additions and 228 deletions.
8 changes: 8 additions & 0 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@

<!--- Describe your changes in detail -->

## Changed Side

<!--- Put an `x` in all the boxes that apply: -->

- [ ] 💻 Backend
- [ ] 📱 Mobile App
- [ ] 🔧 Configuration

## Type of Change

<!--- Put an `x` in all the boxes that apply: -->
Expand Down
60 changes: 60 additions & 0 deletions .github/workflows/build_and_push_backend.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
name: Build backend and push Docker Image

on:
push:
branches:
- dev
paths:
- backend/**
pull_request:
branches:
- dev
paths:
- backend/**

jobs:
deploy:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- uses: dart-lang/setup-dart@v1
with:
sdk: stable

- name: Install Dart Frog
run: dart pub global activate dart_frog_cli

- name: Create Dev Build
run: dart_frog build

- name: Change Directory to build folder
run: cd build/

# - name: Overwrite file
# uses: "DamianReeves/write-file-action@master"
# with:
# path: nixpacks.toml
# write-mode: overwrite
# contents: |
# [phases.setup]
# nixpkgsArchive = 'bc901a14315f03cb02d5be6d7e4c8075cd0fe36c'

- name: Set up QEMU
uses: docker/setup-qemu-action@v2
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2

- name: Login to Docker Hub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_TOKEN }}

- name: Build and push
uses: docker/build-push-action@v4
with:
context: build/
push: true
tags: dungngminh/server:latest
26 changes: 26 additions & 0 deletions backend/docs/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta
name="description"
content="SwaggerUI"
/>
<title>SwaggerUI</title>
<link rel="stylesheet" href="https://unpkg.com/swagger-ui-dist@4.5.0/swagger-ui.css" />
</head>
<body>
<div id="swagger-ui"></div>
<script src="https://unpkg.com/swagger-ui-dist@4.5.0/swagger-ui-bundle.js" crossorigin></script>
<script>
window.onload = () => {
window.ui = SwaggerUIBundle({
url: 'https://very-good-blog-app.up.railway.app/openapi.json',
dom_id: '#swagger-ui',
});
};
</script>
</body>
</html>

2 changes: 1 addition & 1 deletion backend/e2e/auth_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ void main() {
);

final loginResponse =
LoginResponse.fromJson(baseResponse.data as Map<String, dynamic>);
LoginResponse.fromJson(baseResponse.result as Map<String, dynamic>);

expect(response.statusCode, HttpStatus.ok);
expect(
Expand Down
18 changes: 18 additions & 0 deletions backend/lib/common/error_message_code.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class ErrorMessageCode {
ErrorMessageCode._();

// Base
static const unknownError = 'unknown-error';
static const bodyEmpty = 'body-empty';

// Login
static const notRegisterYet = 'not-register-yet';
static const invalidEmailOrPassword = 'invalid-email-or-password';

// Register
static const emailRegisterd = 'email-registerd';


// Blog
static const blocNotFound = 'blog-not-found';
}
3 changes: 2 additions & 1 deletion backend/lib/dtos/response/auth/login_response.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ class LoginResponse {
_$LoginResponseFromJson(json);

final String id;
final String token;
final String token;

Map<String, dynamic> toJson() => _$LoginResponseToJson(this);
}

8 changes: 5 additions & 3 deletions backend/lib/dtos/response/base_pagination_response.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 31 additions & 20 deletions backend/lib/dtos/response/base_response_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,18 @@ part 'base_response_data.g.dart';
class BaseResponseData {
const BaseResponseData({
required this.success,
this.data,
this.result,
this.errorCode,
this.message = kSuccessResponseMessage,
});

factory BaseResponseData.fromJson(Map<String, dynamic> json) =>
_$BaseResponseDataFromJson(json);

factory BaseResponseData.data(dynamic data) {
factory BaseResponseData.data(dynamic result) {
return BaseResponseData(
success: true,
data: data,
result: result,
);
}

Expand All @@ -34,28 +35,32 @@ class BaseResponseData {
);
}

factory BaseResponseData.failed([String? message]) {
factory BaseResponseData.failed({String? errorCode, String? message}) {
return BaseResponseData(
success: false,
errorCode: errorCode,
message: message ?? kFailedResponseMessage,
);
}

final bool success;
final String? errorCode;
final String message;
final dynamic data;
final dynamic result;

Map<String, dynamic> toJson() => _$BaseResponseDataToJson(this);

BaseResponseData copyWith({
bool? success,
String? errorCode,
String? message,
dynamic data,
dynamic result,
}) {
return BaseResponseData(
success: success ?? this.success,
errorCode: errorCode ?? this.errorCode,
message: message ?? this.message,
data: data ?? this.data,
result: result ?? this.result,
);
}
}
Expand All @@ -79,50 +84,56 @@ class CreatedResponse extends Response {
}

class NotFoundResponse extends Response {
NotFoundResponse([String? message])
NotFoundResponse([String? errorCode, String? message])
: super.json(
statusCode: HttpStatus.notFound,
body: BaseResponseData.failed(message).toJson(),
body: BaseResponseData.failed(errorCode: errorCode, message: message)
.toJson(),
);
}

class ConflictResponse extends Response {
ConflictResponse([String? message])
ConflictResponse([String? errorCode, String? message])
: super.json(
statusCode: HttpStatus.conflict,
body: BaseResponseData.failed(message).toJson(),
body: BaseResponseData.failed(errorCode: errorCode, message: message)
.toJson(),
);
}

class UnauthorizedResponse extends Response {
UnauthorizedResponse([String? message])
UnauthorizedResponse([String? errorCode, String? message])
: super.json(
statusCode: HttpStatus.unauthorized,
body: BaseResponseData.failed(message).toJson(),
body: BaseResponseData.failed(errorCode: errorCode, message: message)
.toJson(),
);
}

class BadRequestResponse extends Response {
BadRequestResponse([String? message])
BadRequestResponse([String? errorCode, String? message])
: super.json(
statusCode: HttpStatus.badRequest,
body: BaseResponseData.failed(message).toJson(),
body: BaseResponseData.failed(errorCode: errorCode, message: message)
.toJson(),
);
}

class ForbiddenResponse extends Response {
ForbiddenResponse([String? message])
ForbiddenResponse([String? errorCode, String? message])
: super.json(
statusCode: HttpStatus.forbidden,
body: BaseResponseData.failed(message).toJson(),
body: BaseResponseData.failed(errorCode: errorCode, message: message)
.toJson(),
);
}

class ServerErrorResponse extends Response {
ServerErrorResponse([String? message])
class InternalServerErrorResponse extends Response {
InternalServerErrorResponse([String? errorCode, String? message])
: super.json(
statusCode: HttpStatus.internalServerError,
body: BaseResponseData.failed(message).toJson(),
body: BaseResponseData.failed(errorCode: errorCode, message: message)
.toJson(),
);
}

Expand Down
9 changes: 6 additions & 3 deletions backend/lib/dtos/response/base_response_data.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions backend/lib/dtos/response/blogs/get_blog_response.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:json_annotation/json_annotation.dart';
import 'package:very_good_blog_app_backend/models/following_follower.dart';
import 'package:very_good_blog_app_backend/models/user.dart';

part 'get_user_profile_response.g.dart';

Expand All @@ -15,12 +16,12 @@ class GetUserFollowerResponse {
factory GetUserFollowerResponse.fromJson(Map<String, dynamic> json) =>
_$GetUserFollowerResponseFromJson(json);

factory GetUserFollowerResponse.fromView(FollowingFollowerView view) {
factory GetUserFollowerResponse.fromView(UserView follower) {
return GetUserFollowerResponse(
id: view.follower.id,
fullName: view.follower.fullName,
email: view.follower.email,
avatarUrl: view.follower.avatarUrl,
id: follower.id,
fullName: follower.fullName,
email: follower.email,
avatarUrl: follower.avatarUrl,
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'package:json_annotation/json_annotation.dart';
import 'package:very_good_blog_app_backend/models/following_follower.dart';
import 'package:very_good_blog_app_backend/models/user.dart';

part 'get_user_following_response.g.dart';

Expand All @@ -15,12 +15,12 @@ class GetUserFollowingResponse {
factory GetUserFollowingResponse.fromJson(Map<String, dynamic> json) =>
_$GetUserFollowingResponseFromJson(json);

factory GetUserFollowingResponse.fromView(FollowingFollowerView view) {
factory GetUserFollowingResponse.fromView(UserView following) {
return GetUserFollowingResponse(
id: view.following.id,
fullName: view.following.fullName,
email: view.following.email,
avatarUrl: view.following.avatarUrl,
id: following.id,
fullName: following.fullName,
email: following.email,
avatarUrl: following.avatarUrl,
);
}

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 1901553

Please sign in to comment.