Skip to content

Commit

Permalink
fix: docker flow
Browse files Browse the repository at this point in the history
  • Loading branch information
thuan2172001 committed Mar 13, 2024
1 parent fe70414 commit 189a523
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 64 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ jobs:
runs-on: ubuntu-latest
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
needs: [cargo-check, fmt-check, test-and-coverage]
# needs: [fmt-check]
steps:
- name: Check out from Git
uses: actions/checkout@v4
Expand Down Expand Up @@ -103,7 +102,6 @@ jobs:
runs-on: ubuntu-latest
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
needs: [cargo-check, fmt-check, test-and-coverage]
# needs: [fmt-check]
steps:
- name: Check out from Git
uses: actions/checkout@v4
Expand Down
17 changes: 17 additions & 0 deletions .github/workflows/docker.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: Docker build

on:
pull_request:
branches:
- main

jobs:
build-docker-image:
runs-on: ubuntu-latest
steps:
- name: Check out from Git
uses: actions/checkout@v4

- name: Build docker image
run: |
docker build .
49 changes: 14 additions & 35 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
FROM clux/muslrust:stable AS chef
USER root
RUN cargo install cargo-chef

WORKDIR /app

FROM clux/muslrust:stable AS bunyan
Expand Down
28 changes: 17 additions & 11 deletions src/adapter/src/repositories/postgres/models/question.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use std::time::SystemTime;

use diesel::{AsChangeset, Identifiable, Insertable, Queryable, Selectable};
use rust_core::common::errors::CoreError;
use rust_core::entities::question::QuestionEntity;
use rust_core::entities::question::{QuestionEntity, QuestionId, ToI32};
use serde::Serialize;

#[derive(Debug, Queryable, Serialize, Selectable, Insertable, AsChangeset, Identifiable)]
Expand All @@ -20,23 +19,30 @@ pub struct QuestionModel {
pub created_on: SystemTime,
}

impl QuestionModel {
pub fn from(entity: QuestionEntity) -> Result<Self, CoreError> {
Ok(QuestionModel {
id: entity.id.to_string().parse()?,
impl From<QuestionEntity> for QuestionModel {
fn from(entity: QuestionEntity) -> Self {
let id = entity.id.to_i32().unwrap_or_else(|err| {
eprintln!("Error converting QuestionId to i32: {}", err);
0
});

QuestionModel {
id,
title: entity.title,
content: entity.content,
tags: entity.tags.map(|v| v.into_iter().map(Some).collect()),
created_on: SystemTime::now(),
})
}
}
}

pub fn to_entity(self) -> Result<QuestionEntity, CoreError> {
Ok(QuestionEntity {
id: self.id.to_string().parse()?,
impl Into<QuestionEntity> for QuestionModel {
fn into(self) -> QuestionEntity {
QuestionEntity {
id: QuestionId(self.id.to_string()),
title: self.title,
content: self.content,
tags: self.tags.map(|v| v.into_iter().flatten().collect()),
})
}
}
}
12 changes: 6 additions & 6 deletions src/adapter/src/repositories/postgres/question_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl QuestionPort for QuestionDBRepository {
.await
.unwrap()
.interact(move |conn| {
let question = QuestionModel::from(question).unwrap();
let question = QuestionModel::from(question);
let response = insert_into(questions)
.values(&question)
.get_result::<QuestionModel>(conn)
Expand All @@ -46,7 +46,7 @@ impl QuestionPort for QuestionDBRepository {
_ => CoreError::InternalError,
})
.unwrap();
Ok(response.to_entity().unwrap())
Ok(response.into())
})
.await
.unwrap()
Expand All @@ -58,15 +58,15 @@ impl QuestionPort for QuestionDBRepository {
.await
.unwrap()
.interact(move |conn| {
let question = QuestionModel::from(question)?;
let question = QuestionModel::from(question);
let response = update(questions.filter(id.eq(question.id)))
.set(&question)
.get_result::<QuestionModel>(conn)
.map_err(|err| match err {
diesel::result::Error::NotFound => CoreError::NotFound,
_ => CoreError::InternalError,
})?
.to_entity()?;
.into();

Ok(response)
})
Expand Down Expand Up @@ -109,7 +109,7 @@ impl QuestionPort for QuestionDBRepository {
diesel::result::Error::NotFound => CoreError::NotFound,
_ => CoreError::InternalError,
})?
.to_entity()?;
.into();

Ok(response)
})
Expand All @@ -136,7 +136,7 @@ impl QuestionPort for QuestionDBRepository {

Ok(question_list
.into_iter()
.map(|l| l.to_entity().unwrap())
.map(|l: QuestionModel| l.into())
.collect::<Vec<_>>())
})
.await
Expand Down
18 changes: 18 additions & 0 deletions src/core/src/entities/question.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,24 @@ impl FromStr for QuestionId {
}
}

pub trait ToI32 {
type Err;

fn to_i32(self) -> Result<i32, Self::Err>;
}

impl ToI32 for QuestionId {
type Err = Error;

fn to_i32(self: QuestionId) -> Result<i32, Error> {
let id = self
.0
.parse()
.map_err(|_| Error::new(ErrorKind::InvalidData, "Cannot parse ID into i32"));
Ok(id.unwrap())
}
}

impl fmt::Display for QuestionId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
Expand Down
9 changes: 0 additions & 9 deletions src/grpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,9 @@ autobenches = true
[dependencies.tonic]
version = "0.11.0"

[dependencies.once_cell]
version = "1.8.0"

[dependencies.prost]
version = "0.12.3"

[dependencies.prost-build]
version = "0.12.3"

[dependencies.protobuf-src]
version = "1.1.0"

[dependencies.rust_core]
path = "../core"

Expand Down
1 change: 0 additions & 1 deletion src/public/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#[rustfmt::skip]
#[cfg_attr(debug_assertions, allow(dead_code, unused_imports))]
use openssl;
#[rustfmt::skip]
Expand Down

0 comments on commit 189a523

Please sign in to comment.