Skip to content

pershin-daniil/userapi_assessment

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Refactoring User API in Go 💙

This is a small API that handles User entity, where the storage is a JSON file.

Requirements 😏

  • The storage must remain a JSON file.
  • User structure should not be reduced.
  • The application should not lose any existing functionality.

Installation ✅

Clone the repository and download dependencies.

git clone [email protected]:pershin-daniil/userapi_assessment.git
cd userapi_assessment
go mod tidy

Use make run - to start service locally on localhost:3333 OR make rund - to start service in docker on localhost:8080.

If you want to test service use command below and then use scripts in API Endpoints.

Also, you can run service locally and test it with http requests here

make rund

API Endpoints 🖊

GET /users

Returns a list of all users in the storage.

Request

curl --location 'http://localhost:8080/api/v1/users'

Response

{
  "increment": 2,
  "list": {
    "1": {
      "id": 1,
      "displayName": "Ivan",
      "email": "[email protected]",
      "created": "2023-04-16T19:04:53.525308741Z",
      "updated": "2023-04-16T19:04:53.525308741Z"
    },
    "2": {
      "id": 2,
      "displayName": "Ivan",
      "email": "[email protected]",
      "created": "2023-04-16T19:07:52.723930891Z",
      "updated": "2023-04-16T19:07:52.723930891Z"
    }
  }
}

POST /users

Adds a new user to the storage.

Request

curl --location 'localhost:8080/api/v1/users' \
--header 'Content-Type: application/json' \
--data-raw '{
  "displayName": "Ivan",
  "email": "[email protected]"
}'

Response

{
  "id": 1,
  "displayName": "Ivan",
  "email": "[email protected]",
  "created": "2023-04-16T19:04:53.525308741Z",
  "updated": "2023-04-16T19:04:53.525308741Z"
}

GET /users/{id}

Returns a specific user by ID.

Request

curl --location 'localhost:8080/api/v1/users/1'

Response

{
  "id": 1,
  "displayName": "Ivan",
  "email": "[email protected]",
  "created": "2023-04-16T19:04:53.525308741Z",
  "updated": "2023-04-16T19:04:53.525308741Z"
}

PATCH /users/{id}

Updates an existing user by ID. You can change only displayName.

Request

curl --location --request PATCH 'localhost:8080/api/v1/users/1' \
--header 'Content-Type: application/json' \
--data '{
  "displayName": "MASHA"
}'

Response

{
  "id": 1,
  "displayName": "MASHA",
  "email": "[email protected]",
  "created": "2023-04-16T19:04:53.525308741Z",
  "updated": "2023-04-16T19:28:45.540040396Z"
}

DELETE /users/{id}

Deletes a user from the storage by ID.

Request

curl --location --request DELETE 'localhost:8080/api/v1/users/4'

Response

Status: 204 No Content

Useful information 🤔

👉 Full task text here

👉 Make file commands

lint:
	gofumpt -w .
	go mod tidy
	golangci-lint run

run:
	go run cmd/userapi/main.go

test:
	go test -v ./tests/userapi_test.go

build:
	docker build -f ./deploy/local/Dockerfile -t userapi .

rund: build
	docker run --rm --name userapi -p 8080:3333 userapi

About

TrueConf test case for junior golang developer.

Resources

Stars

Watchers

Forks