Categorygithub.com/doanlng/Go-Api-Tech-Challenge
modulepackage
0.0.0-20241009201333-5f150f6a4b6a
Repository: https://github.com/doanlng/go-api-tech-challenge.git
Documentation: pkg.go.dev

# README

CapTech Banner

Go API Tech Challenge: Assignment

Table of Contents

Setting up a database instance

For this Tech Challenge, we will be using Postgres running in a docker container as our database. This has already been configured for you using a docker-compose file. To start the data base, run the following:

make db_up

Tech Challenge Assignment

Summary

For this Tech Challenge, you will create a web API that exposes endpoints that read from and write to a database that represents a fictional college and contains courses and people.

Web Framework

You have the freedom to build this API with whatever tool you would like. With that being said, you are strongly encouraged to use the standard library and/or chi where possible.

Data

The data for this project lives inside of the database we created in the previous section. As part of this Tech Challenge, you will need to access this data.

Please note that you will need to establish relationships between each table in order to complete this challenge.

Project Structure

Your project will need to define three routes, one each for courses and person. Each route will need to include handlers for get all, get by id, update by id, add, and delete actions.

You should follow idiomatic go principles for your project. This includes project structure, code organization, and naming conventions.

Your final product should include the web server, a dockerfile, and integration for that docker file in the provided docker compose. You should also update the makefile with any needed commands to get your app running. In addition, any documentation that an outside developer would need to get your app up and running should be included.

Bellow are further details for each endpoint.


api/course

Request TypeEndpointQuery ParametersRequest BodyResponse TypeInstructions
GEThttp://localhost:8000/api/coursenonenoneJSON-formatted string representing  a list of  Course objectsReturn all Course objects from the database.
GEThttp://localhost:8000/api/course/{id}nonenoneJSON-formatted string representing  a Course objectReturn a given Course object based on id.
PUThttp://localhost:8000/api/course/{id}noneJSON-formatted string representing a Course objectJSON-formatted string representing  an updated Course objectUpdate a given Course object in the database based on id. The Course object passed to the endpoint should be validated.
POSThttp://localhost:8000/api/coursenoneJSON-formatted string representing a Course objectJSON-formatted string representing  a the new Course object's idAdd a new Course object to the database. id does not need to be provided as the database will generate it.
DELETEhttp://localhost:8000/api/course/{id}nonenoneJSON-formatted string representing  a deletion confirmation messageDelete a given Course object from the database based on id.

Here is the schema for a Course object

Column NameColumn Type
idinteger
namestring

api/person

Request TypeEndpointQuery ParametersRequest BodyResponse TypeInstructions
GEThttp://localhost:8000/api/personname: string
age: integer
noneJSON-formatted string representing  a list of  Person objectsReturn all People objects from the database. If query parameters are passed to the endpoint, filter off of them.
GEThttp://localhost:8000/api/person/{name}nonenoneJSON-formatted string representing  a Person objectReturn a given Person based off of name.
PUThttp://localhost:8000/api/person/{name}noneJSON-formatted string representing a Person objectJSON-formatted string representing  an updated Person objectUpdate a given Person in the database based on name. The Person object passed to the endpoint should be validated.
POSThttp://localhost:8000/api/personnoneJSON-formatted string representing a Person objectJSON-formatted string representing  a the new Person object's idAdd a new Person to the database. id does not need to be provided as the database will generate it. If any Course objects ids are passed in, that association should be updated in the database.
DELETEhttp://localhost:8000/api/person/{name}nonenoneJSON-formatted string representing  a deletion confirmation messageDelete a given Person object from the database based on name.

Here is the schema for a Person object:

Column NameColumn TypeNotes
idintegerprimary key
first_namestringN/A
last_namestringN/A
typestringpossible values are student and professor
ageintegerN/A
courseslist of integerslist of course ids

Project Requirements Checklist

  • Your API should use port 8000.
  • Your API should have a single entry point.
  • Each endpoint should return the appropriate statues code with each response.
  • If an error is encountered by the application, an informative error message should be returned to the client and your application should also log details.
  • Your project should include unit tests with 80% unit test coverage.

https://medium.com/@rseanjustice/data-access-in-go-d39d8945b078 https://go.dev/doc/tutorial/database-access https://www.calhoun.io/connecting-to-a-postgresql-database-with-gos-database-sql-package/ https://medium.com/@leeprovoost/get-lastinsertedid-when-using-postgres-and-golang-database-sql-package-ca55ea23c2d5 https://www.sitepoint.com/get-url-parameters-with-go/