Skip to content

fac30/PRO05_BACK_Gaj_Khalos_Max

Repository files navigation

PokeLike API

Pikachu welcomes you!

Welcome to the PokeLike API! This project helps you track Pokémon you find cute 🥰 using an ASP.NET Core Web API backed by a PostgreSQL database.

Table of Contents


Getting Started

Prerequisites

Installation

  1. Clone the repository:
    git clone https://github.com/fac30/PRO05_BACK_Gaj_Khalos_Max.git
    cd PRO05_BACK_Gaj_Khalos_Max

Database Setup

Using Docker

When using Docker, the PostgreSQL database is automatically set up in a container. No additional manual configuration is required.

  1. Pull the Docker image from Docker Hub:

    docker-compose pull
  2. Build and start the Docker containers:

    docker-compose up -d
  3. The API will be available at http://localhost/.

  4. To stop the containers, run:

    docker-compose down
  5. The API will be available at http://localhost/ - no port number is needed.

Running Locally

To start the application locally, follow these steps:

  1. Modify ConnectionString in appsettings.json to be hosted on localhost rather than db Docker container:

    "ConnectionStrings": {
     "PokeLikeDbContext": "Host=localhost;Port=5432;Database=PokeLikeDb;Username=postgres;Password=password"
    }
  2. Restore dependencies:

    dotnet restore
  3. Run the application:

    dotnet run

Project Structure

  • Program.cs: Configures services and middleware for the application.
  • Data/PokeLikeDbContext.cs: EF Core DbContext configuration for managing the database context.
  • Models/: Contains entity models such as Pokemon and Collection.
  • Migrations/: Contains EF Core migration files.

Configuration

  • Naming Conventions: The project uses the EFCore.NamingConventions plugin for consistent naming. By default, snake_case is applied to all table and column names.

    builder.Services.AddDbContextPool<PokeLikeDbContext>(opt =>
        opt.UseNpgsql(builder.Configuration.GetConnectionString("PokeLikeDbContext"))
           .UseSnakeCaseNamingConvention());

Entity Framework Migrations

Adding a New Migration

To add a new migration after modifying models, run:

dotnet ef migrations add <MigrationName>

Removing the Last Migration

If you need to undo the last migration:

dotnet ef migrations remove

Applying Migrations

Ensure the latest migrations are applied to the database:

dotnet ef database update

API Endpoints

Base URL

  • Docker: http://localhost:8080
  • Local Development: http://localhost:5050

Endpoints

  • GET /pokemon: Retrieves a list of Pokémon.
  • POST /pokemon: Adds a new Pokémon.
    • Body:
      {
        "name": "Pikachu",
        "imageUrl": "https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/other/dream-world/25.svg",
        "apiUrl": "https://pokeapi.co/api/v2/pokemon/pikachu",
        "likes": 100
      }

Troubleshooting

Docker Issues

  • Port conflicts: Ensure no other services are running on the ports specified in docker-compose.yml.

  • Database connection issues: If the application cannot connect to the database, verify that the database container is running:

    docker ps
  • Resetting Docker containers: To rebuild and reset all containers:

    docker-compose down -v
    docker system prune -a
    docker-compose up --build
  • Resetting migration If there have been updates to the codebase, remove migrations and re-initialise them before re-running container commands:

    dotnet ef migrations remove
    dotnet ef migrations add InitialCreate
  • Pulling Docker image from Docker Hub If you have trouble authenticating and pulling the image from Docker Hub, you may need to login with Docker:

    docker login

About

Backend for our pokemon collection project

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published