Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: docker flow
Browse files Browse the repository at this point in the history
thuan2172001 committed Mar 14, 2024

Verified

This commit was signed with the committer’s verified signature.
thuan2172001 Trịnh Văn Thuận
1 parent fe70414 commit 6daceb0
Showing 9 changed files with 73 additions and 63 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -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
@@ -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
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
@@ -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
25 changes: 15 additions & 10 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 std::{io::Error, 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)]
@@ -20,23 +19,29 @@ pub struct QuestionModel {
pub created_on: SystemTime,
}

impl QuestionModel {
pub fn from(entity: QuestionEntity) -> Result<Self, CoreError> {
impl TryFrom<QuestionEntity> for QuestionModel {
type Error = Error;

fn try_from(entity: QuestionEntity) -> Result<QuestionModel, Self::Error> {
let id = entity.id.to_i32()?;

Ok(QuestionModel {
id: entity.id.to_string().parse()?,
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()),
})
}
}
}
14 changes: 8 additions & 6 deletions src/adapter/src/repositories/postgres/question_db.rs
Original file line number Diff line number Diff line change
@@ -37,7 +37,8 @@ impl QuestionPort for QuestionDBRepository {
.await
.unwrap()
.interact(move |conn| {
let question = QuestionModel::from(question).unwrap();
let question =
QuestionModel::try_from(question).map_err(|_| CoreError::InternalError)?;
let response = insert_into(questions)
.values(&question)
.get_result::<QuestionModel>(conn)
@@ -46,7 +47,7 @@ impl QuestionPort for QuestionDBRepository {
_ => CoreError::InternalError,
})
.unwrap();
Ok(response.to_entity().unwrap())
Ok(response.into())
})
.await
.unwrap()
@@ -58,15 +59,16 @@ impl QuestionPort for QuestionDBRepository {
.await
.unwrap()
.interact(move |conn| {
let question = QuestionModel::from(question)?;
let question =
QuestionModel::try_from(question).map_err(|_| CoreError::InternalError)?;
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)
})
@@ -109,7 +111,7 @@ impl QuestionPort for QuestionDBRepository {
diesel::result::Error::NotFound => CoreError::NotFound,
_ => CoreError::InternalError,
})?
.to_entity()?;
.into();

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

Ok(question_list
.into_iter()
.map(|l| l.to_entity().unwrap())
.map(|l: QuestionModel| l.into())
.collect::<Vec<_>>())
})
.await
18 changes: 18 additions & 0 deletions src/core/src/entities/question.rs
Original file line number Diff line number Diff line change
@@ -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)
9 changes: 0 additions & 9 deletions src/grpc/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"

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]

0 comments on commit 6daceb0

Please sign in to comment.