The Company API is a .NET Core-based web service that allows for the management of company records. This API supports creating, retrieving, updating, and listing company records. It enforces uniqueness on the ISIN field and includes authentication for secure access.
- Create: Add a new company with Name, Stock Ticker, Exchange, ISIN, and optional Website URL.
- Read: Retrieve a company by ID or ISIN.
- List: Retrieve a collection of all companies.
- Update: Modify existing company details.
- Authentication: Secure API endpoints with Bearer Token authentication.
- Backend: ASP.NET Core 8 Web API
- Frontend: ASP.NET Core MVC (for the client application)
- Database: SQL Server (production) / In-Memory Database (testing)
- Testing: xUnit, Moq
- Authentication: Bearer Token Authentication
To set up the database for this project, follow these steps:
Ensure you have the Entity Framework Core tools installed. This will allow you to create and apply migrations. Run the following command to install the tools globally if you haven't already:
dotnet tool install --global dotnet-ef
Since the 20240826184330_InitialCreate
migration already exists, you need to apply this migration to your database to create the initial schema.
Run the following command to apply the migration:
dotnet ef database update
This command will apply all pending migrations, including 20240826184330_InitialCreate
, to the database and create the database if it doesn’t already exist.
After applying the migration, verify that the database schema is created as expected. You can check the database using a database management tool such as SQL Server Management Studio (SSMS) or any other SQL client tool.
Ensure that your connection string is correctly configured in appsettings.json
or your environment variables. Here is an example configuration in appsettings.json
:
{
"ConnectionStrings": {
"DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=CompanyDb;Trusted_Connection=True;MultipleActiveResultSets=true"
}
}
Replace Server
, Database
, and other connection details according to your database setup.
CREATE TABLE Companies (
Id INT IDENTITY(1,1) PRIMARY KEY,
Name NVARCHAR(100) NOT NULL,
Exchange NVARCHAR(100) NOT NULL,
Ticker NVARCHAR(10) NOT NULL,
Isin NVARCHAR(12) NOT NULL UNIQUE,
Website NVARCHAR(255) NULL
);
To set up the database schema, use the following SQL script:
-- Create the database
CREATE DATABASE CompanyDb;
-- Use the created database
USE CompanyDb;
-- Create the Companies table
CREATE TABLE Companies (
Id INT IDENTITY(1,1) PRIMARY KEY,
Name NVARCHAR(100) NOT NULL,
Exchange NVARCHAR(100) NOT NULL,
Ticker NVARCHAR(10) NOT NULL,
Isin NVARCHAR(12) NOT NULL UNIQUE,
Website NVARCHAR(255) NULL
);
- POST
/api/companies
- Create a new company - GET
/api/companies
- Retrieve a list of all companies - GET
/api/companies/{id}
- Retrieve a specific company by ID - GET
/api/companies/isin/{isin}
- Retrieve a specific company by ISIN - PUT
/api/companies/{id}
- Update an existing company
The API uses Bearer Token authentication. Include the token in the Authorization
header as Bearer <token>
.
-
Clone the Repository
git clone https://github.com/thisismck/CompanyApi.git cd CompanyApi
-
Build and Run the API
dotnet build dotnet run
The API will be available at
http://localhost:7110
. -
Build and Run the Client
Navigate to the
CompanyClient
directory and run:dotnet build dotnet run
The client application will be available at
http://localhost:7027
.
Navigate to the CompanyApi.Tests
directory and run:
dotnet test
- Deploy the API to a server or cloud service that supports .NET Core applications.
- Set up SQL Server and run the provided SQL scripts to create the database schema.
- Configure the connection strings and authentication settings as required.
Ensure the following configuration settings are defined in appsettings.json
or environment variables:
- Connection String: for SQL Server
- JWT Settings: for Bearer Token authentication
The project includes a simple ASP.NET Core MVC client that interacts with the API and displays results in a browser.
The API is secured using Bearer Token authentication. Tokens must be included in the Authorization
header of requests.