Skip to content
This repository was archived by the owner on May 20, 2024. It is now read-only.

Feature/#9 devlopment v1.0 #11

Merged
merged 20 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
7841957
Move .golangci.yml and update Dockerfile accordingly
taiki-kuraishi Feb 7, 2024
e743fef
Implement User-related operations in api-server #9
taiki-kuraishi Feb 7, 2024
7725e18
Implement Entry-related operations in api-server #9
taiki-kuraishi Feb 7, 2024
f225ba8
Add user validator in api-server #9
taiki-kuraishi Feb 8, 2024
a636e44
Add entry validator in api-server #9
taiki-kuraishi Feb 8, 2024
8d80f3d
Add tests for entry_validator.go in spi-server #9
taiki-kuraishi Feb 9, 2024
510910f
Add tests for user_validator.go in spi-server #9
taiki-kuraishi Feb 9, 2024
71d2f6b
Refactor validator tests to use interface
taiki-kuraishi Feb 10, 2024
5b7b480
Add tests for ewpository in spi-server
taiki-kuraishi Feb 10, 2024
a3095bd
Refactor validator tests into subtests in api-server #9
taiki-kuraishi Feb 10, 2024
cad6c8a
Add tests for ewpository in spi-server #9
taiki-kuraishi Feb 10, 2024
1d7fc05
Refactor names of test functions in api-server repository #9
taiki-kuraishi Feb 10, 2024
07524c6
Add tests for usecase in spi-server #9
taiki-kuraishi Feb 10, 2024
2220d99
Add tests for controller in spi-server #9
taiki-kuraishi Feb 11, 2024
1ed854f
Add tests for model in spi-server #9
taiki-kuraishi Feb 11, 2024
82ff54d
Add tests for migrate in spi-server #9
taiki-kuraishi Feb 11, 2024
1f48684
Add tests for router in spi-server #9
taiki-kuraishi Feb 11, 2024
682e2c2
Add send_request_to_api function in nfc_reader #9
taiki-kuraishi Feb 11, 2024
396bd07
Add api-server tests to pre-commit and GitHub Actions
taiki-kuraishi Feb 13, 2024
dd9a7ce
Add healthcheck dokcer-compose.yml #9
taiki-kuraishi Feb 13, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .github/workflows/Code_quality_and_docker_build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,11 @@ jobs:
- name: Create api/.env.api
run: |
touch api/.env.api
echo "TIME_VALIDATION_MIN=${{ secrets.TIME_VALIDATION_MIN }}" >> api/.env.api
echo "STUDENT_NUMBER_MIN=${{ secrets.STUDENT_NUMBER_MIN }}" >> api/.env.api
echo "STUDENT_NUMBER_MAX=${{ secrets.STUDENT_NUMBER_MAX }}" >> api/.env.api
echo "NAME_MIN_LENGTH=${{ secrets.NAME_MIN_LENGTH }}" >> api/.env.api
echo "NAME_MAX_LENGTH=${{ secrets.NAME_MAX_LENGTH }}" >> api/.env.api
echo "MYSQL_USER=${{ secrets.MYSQL_USER }}" >> api/.env.api
echo "MYSQL_PASSWORD=${{ secrets.MYSQL_PASSWORD }}" >> api/.env.api
echo "MYSQL_HOST=${{ secrets.MYSQL_HOST }}" >> api/.env.api
Expand All @@ -159,6 +164,7 @@ jobs:
echo "NFC_SERVICE_CODE=${{ secrets.NFC_SERVICE_CODE }}" >> nfc_reader/.env.nfc_reader
echo "NFC_STUDENT_NUM_BLOCK_CODE=${{ secrets.NFC_STUDENT_NUM_BLOCK_CODE }}" >> nfc_reader/.env.nfc_reader
echo "NFC_NAME_BLOCK_CODE=${{ secrets.NFC_NAME_BLOCK_CODE }}" >> nfc_reader/.env.nfc_reader
echo "API_URL=${{ secrets.API_URL }}" >> nfc_reader/.env.nfc_reader

- name: Build docker image
run: sudo docker-compose build
33 changes: 33 additions & 0 deletions .github/workflows/Run_api_server_tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Run api server tests

on:
push:
branches:
- main
pull_request:

jobs:
run-api-server-test:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Create api/.env.api
run: |
touch api/.env.api
echo "TIME_VALIDATION_MIN=${{ secrets.TIME_VALIDATION_MIN }}" >> api/.env.api
echo "STUDENT_NUMBER_MIN=${{ secrets.STUDENT_NUMBER_MIN }}" >> api/.env.api
echo "STUDENT_NUMBER_MAX=${{ secrets.STUDENT_NUMBER_MAX }}" >> api/.env.api
echo "NAME_MIN_LENGTH=${{ secrets.NAME_MIN_LENGTH }}" >> api/.env.api
echo "NAME_MAX_LENGTH=${{ secrets.NAME_MAX_LENGTH }}" >> api/.env.api
echo "MYSQL_USER=${{ secrets.MYSQL_USER }}" >> api/.env.api
echo "MYSQL_PASSWORD=${{ secrets.MYSQL_PASSWORD }}" >> api/.env.api
echo "MYSQL_HOST=${{ secrets.MYSQL_HOST }}" >> api/.env.api
echo "MYSQL_DATABASE=${{ secrets.MYSQL_DATABASE }}" >> api/.env.api

- name: Run Go tests
run: |
cd api/ && sudo docker build --target test -t nfc-entry-management-api-test:latest -f ./Dockerfile .
sudo docker run --env-file .env.api --rm nfc-entry-management-api-test:latest
9 changes: 9 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,15 @@ repos:
language: system
types: [python]

- repo: local
hooks:
- id: go-tests
name: Run Go tests
entry: sh -c 'cd api/ && docker build --target test -t nfc-entry-management-api-test:latest -f ./Dockerfile . && docker run --env-file .env.api --rm nfc-entry-management-api-test:latest'
language: system
pass_filenames: false
stages: [commit]

- repo: local
hooks:
- id: docker-compose-check
Expand Down
5 changes: 5 additions & 0 deletions api/.env.api.example
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
TIME_VALIDATION_MIN=1704034800
STUDENT_NUMBER_MIN=10000000
STUDENT_NUMBER_MAX=39999999
NAME_MIN_LENGTH=2
NAME_MAX_LENGTH=32
MYSQL_USER=admin
MYSQL_PASSWORD=password
MYSQL_HOST=mysql
Expand Down
16 changes: 10 additions & 6 deletions api/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ FROM golang:1.21.6-alpine AS base

WORKDIR /app

COPY ./src /app/src
COPY ./src .

WORKDIR /app/src

RUN apk add --no-cache git && \
go install -v golang.org/x/tools/cmd/goimports@latest && \
Expand All @@ -14,18 +13,23 @@ RUN go mod tidy

ENV PATH="/app/bin:${PATH}"

# test stage
FROM base AS test

WORKDIR /app

CMD [ "go","test","-v","./..." ]

# development stage
FROM base AS development

WORKDIR /app

COPY ./.golangci.yml .

RUN wget -O- -nv https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b /usr/local/bin v1.55.2

# lint stage
FROM development AS lint

WORKDIR /app/src
WORKDIR /app

CMD [ "golangci-lint","run","--config","/app/.golangci.yml" ]
CMD [ "golangci-lint","run","--config",".golangci.yml" ]
File renamed without changes.
84 changes: 84 additions & 0 deletions api/src/controller/api_controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package controller

import (
"api/model"
"api/usecase"
"fmt"
"net/http"
"time"

"github.com/labstack/echo"
)

type IUserAndEntryController interface {
HandleUserAndEntry(c echo.Context) error
}

type UserAndEntryController struct {
uu usecase.IUserUsecase
eu usecase.IEntryUsecase
}

type Response struct {
UserMessage string `json:"user_message"`
EntryMessage string `json:"entry_message"`
}

type Request struct {
StudentNumber uint `json:"student_number"`
Name string `json:"name"`
Timestamp float64 `json:"timestamp"`
}

func NewUserAndEntryController(uu usecase.IUserUsecase, eu usecase.IEntryUsecase) IUserAndEntryController {
return &UserAndEntryController{uu, eu}
}

func (ac *UserAndEntryController) HandleUserAndEntry(c echo.Context) error {
request := Request{}
if err := c.Bind(&request); err != nil {
fmt.Println(err.Error())
return c.JSON(http.StatusBadRequest, err.Error())
}

if request.StudentNumber == 0 {
fmt.Println("student number is required")
return c.JSON(http.StatusBadRequest, "student number is required")
}
if request.Name == "" {
return c.JSON(http.StatusBadRequest, "name is required")
}
if request.Timestamp == 0 {
return c.JSON(http.StatusBadRequest, "timestamp is required")
}

// convert float64 to time.Time
seconds := int64(request.Timestamp)
nanoseconds := int64((request.Timestamp - float64(seconds)) * 1e9)
timestamp := time.Unix(seconds, nanoseconds)

user := model.User{
StudentNumber: request.StudentNumber,
Name: request.Name,
CreatedAt: timestamp,
UpdatedAt: timestamp,
}

userMessage, err := ac.uu.CreateOrUpdateUser(user)
if err != nil {
fmt.Println(err.Error())
return c.JSON(http.StatusInternalServerError, err.Error())
}

entryMessage, err := ac.eu.EntryOrExit(request.StudentNumber, timestamp)
if err != nil {
return c.JSON(http.StatusInternalServerError, err.Error())
}

response := Response{
UserMessage: userMessage,
EntryMessage: entryMessage,
}

return c.JSON(http.StatusOK, response)
}
Loading
Loading