Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/nikoletaxvs/countriesapi
REST API in .net 6 ,using redis for caching
https://github.com/nikoletaxvs/countriesapi
cache-redis dotnet-6 rest-api
Last synced: 12 days ago
JSON representation
REST API in .net 6 ,using redis for caching
- Host: GitHub
- URL: https://github.com/nikoletaxvs/countriesapi
- Owner: nikoletaxvs
- Created: 2023-12-22T12:52:49.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2024-08-03T07:10:29.000Z (5 months ago)
- Last Synced: 2024-11-05T13:13:00.312Z (about 2 months ago)
- Topics: cache-redis, dotnet-6, rest-api
- Language: C#
- Homepage:
- Size: 7.95 MB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Countries API## Table of Contents
- [Introduction](#introduction)
- [Installation](#installation)
- [Endpoints](#endpoints)
- [Architecture](#architecture)
- [Testing](#testing)## Introduction
This REST API is exposing FindSecondLargest and GetCountries endpoints.Its fetching it's data for the latter from the 3rd party api
Rest Countries and saves it to a cache using a Docker Redis container.
It also stores the data inside a Microsoft SQL Server database.
Finally the endpoints are unit tested.
## InstallationFollow these 5 easy steps to install and set up the project:
###### Step 1. Clone the Repository
```bash
# Clone the repository
git clone https://github.com/nikoletaxvs/Assessment
cd Assessment
```
###### Step2 .Install necessary NuGet packages
```bash
#Restore NuGet Packages
dotnet restore
```
###### Step 3.Create a database migration
```bash
# Create a database migration
dotnet ef migrations add InitialCreate -c ApplicationDbContext# Update the database
dotnet ef database update -c ApplicationDbContext
```
###### Step 4.Setup Redis with Docker
```bash
#If redis is not pulled yet, open powershell as an administrator and run, otherwise skip this step
docker pull redis#Then run
docker run -p 6379:6379 --name my-redis -d redis #if that's done use docker start
```###### Step 5.Build and Run the API
```bash
# Build the project
dotnet build# Run the API
dotnet run
```## Endpoints
##### 1. FindSecondLargest
Endpoint: POST /FindSecondLargest
###### Input:
A JSON body of RequestObj.
###### Output:
The second largest integer of the array gets returned.
###### Response:
Status 200: Success. Returns the second largest integer.
Status 400: Bad Request. The given array should have at least two integers.
Status 500: Internal Server Error.###### Example:
```json{
"RequestArrayObj": [4, 7, 1, 9, 3]
}
```
###### Response:
```json
{
"secondLargest": 7
}
```##### 2. GetCountries
Endpoint: GET /GetCountries
###### Output:
IEnumerable with common name, capital, and borders as its fields, retrieved from the third-party API, cache, or the database.
###### Response:
Status 200: Success. Returns the list of country dtos.
Status 500: Internal Server Error.
###### Example:
###### Response:```json
[
{
"commonName": "string",
"capital": "string",
"borders": [
{
"name": "string"
}
]
}
]
```
##### 3. DeleteCountriesUtilityEndpoint: GET /DeleteAllCountriesUtility
###### Description:
This utility endpoint deletes all countries and their borders.###### Response:
Status 200: Success. Deleted all countries and their borders.
Status 400: Bad Request. There was some issue.
Status 500: Internal Server Error.
## Architecture##### Database
The `Country` class has a one-to-many relationship with the `Border` class
```mermaid
erDiagram
Country ||--o{ Border : "1" Id
Country {
int Id
string CommonName
string Capital
}
Border {
int Id
string Name
int CountryId
}```
##### Classes```mermaid
classDiagram
ApplicationDbContext --> CountryRepository :Dependency Injection
ICountryApiService <|-- CountryApiService
ICacheService <|-- CacheService
ICountryRepository <|-- CountryRepository
CountryRepository --> AssesmentController: Dependency Injection
CountryApiService --> AssesmentController: Dependency Injection
CacheService --> AssesmentController: Dependency Injection
```
In the application's architecture:- **Data Layer:** The `ApplicationDbContext` facilitates database operations by injecting the `CountryRepository`.
- **Service Layer:** Services (`CountryApiService`, `CacheService`, `CountryRepository`) implement interfaces (`ICountryApiService`, `ICacheService`, `ICountryRepository`) for structured functionality.
- **Dependency Injection:** Services are injected into `AssesmentController` to manage HTTP requests. This design emphasizes modularity and separation of concerns, enhancing the application's maintainability and scalability.## Testing
This rest api has been tested with XUnit. The xunit test project can be found in this repository : Tests Repository