The Contact Manager Project is a RESTful web API built with ASP.NET Core that allows users to manage their contacts. Users can perform CRUD (Create, Read, Update, Delete) operations on their contacts using this API.
- .NET 6 SDK or later
- PostgreSQL
The project uses a PostgreSQL database to store contact information. The database schema is defined in the Contacts
table, which has the following columns:
Id
(integer, primary key): unique identifier for the contactSalutation
(text, not null): title or form of address for the contactFirstName
(text, not null): first name of the contactLastName
(text, not null): last name of the contactDisplayName
(text): name to display for the contact (defaults to concatenation ofSalutation
,FirstName
, andLastName
)Birthdate
(date): date of birth for the contactCreationTimestamp
(timestamp, not null): timestamp of when the contact was createdLastChangeTimestamp
(timestamp): timestamp of when the contact was last updatedNotifyHasBirthdaySoon
(boolean): whether or not to notify the user when the contact has a birthday soonEmail
(text, not null, unique): email address of the contactPhoneNumber
(text): phone number of the contact
The project includes SQL scripts to create the Contacts
table, as well as functions and triggers to update the DisplayName
, LastChangeTimestamp
, and NotifyHasBirthdaySoon
columns automatically.
CREATE TABLE Contacts (
Id SERIAL PRIMARY KEY,
Salutation TEXT NOT NULL CHECK (length(Salutation) > 2),
FirstName TEXT NOT NULL CHECK (length(FirstName) > 2),
LastName TEXT NOT NULL CHECK (length(LastName) > 2),
DisplayName TEXT DEFAULT '',
Birthdate DATE,
CreationTimestamp TIMESTAMP NOT NULL DEFAULT NOW(),
LastChangeTimestamp TIMESTAMP,
NotifyHasBirthdaySoon BOOLEAN DEFAULT FALSE,
Email TEXT NOT NULL UNIQUE,
PhoneNumber TEXT
);
CREATE OR REPLACE FUNCTION update_display_name()
RETURNS TRIGGER AS $$
BEGIN
IF NEW.DisplayName = '' THEN
NEW.DisplayName := NEW.Salutation || ' ' || NEW.FirstName || ' ' || NEW.LastName;
END IF;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER update_display_name
BEFORE INSERT ON Contacts
FOR EACH ROW
EXECUTE FUNCTION update_display_name();
CREATE OR REPLACE FUNCTION update_last_change_timestamp()
RETURNS TRIGGER AS $$
BEGIN
NEW.LastChangeTimestamp := NOW();
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER update_last_change_timestamp
BEFORE UPDATE ON Contacts
FOR EACH ROW
EXECUTE FUNCTION update_last_change_timestamp();
CREATE OR REPLACE FUNCTION update_notify_birthday_soon()
RETURNS TRIGGER AS $$
BEGIN
IF NEW.Birthdate IS NOT NULL THEN
IF date_trunc('year', age(NEW.Birthdate)) != date_trunc('year', age(NEW.Birthdate - interval '14 days')) THEN
NEW.NotifyHasBirthdaySoon := TRUE;
ELSE
NEW.NotifyHasBirthdaySoon := FALSE;
END IF;
ELSE
NEW.NotifyHasBirthdaySoon := FALSE;
END IF;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER update_notify_birthday_soon
BEFORE INSERT OR UPDATE ON Contacts
FOR EACH ROW
EXECUTE FUNCTION update_notify_birthday_soon();
INSERT INTO Contacts (Id, Salutation, FirstName, LastName, Email, Birthdate)
VALUES
(1, 'Herr', 'Niko', 'Laus', 'niko@laus.com', '2002-02-10'),
(2, 'Frau', 'Martina', 'Martell', 'martina@martell.com', '1968-03-30'),
(3, 'Dr.', 'Robert', 'Pfeiffer', 'robert@pfeiffer.com', '1989-03-18'),
(4, 'Dr.', 'John', 'Schnee', 'john@schnee.com', CURRENT_DATE - interval '1 year' + interval '5 day'),
(5, 'Herr', 'Michael', 'Henker', 'michael@henker.com', '1971-07-07');
SELECT * FROM Contacts;
The project has been structured into the following folders:
ContactManager
: Contains the main project files.DataAccessLibrary
: Contains the data access layer for the project.ContactManager.DependencyInjection
: Contains the dependency injection configuration for the project.
The following API endpoints are available:
GET /api/contact
: Returns a list of all contacts.GET /api/contact/{contactId}
: Returns a single contact by ID.POST /api/contact
: Creates a new contact.PUT /api/contact
: Updates an existing contact.DELETE /api/contact/{contactId}
: Deletes a contact by ID.
The following NuGet packages are used in this project:
- Microsoft.AspNetCore.App
- Npgsql
- Dapper
- Swashbuckle.AspNetCore
To run the project, follow these steps:
- Clone the project to your local machine.
- Open the project in Visual Studio 2019 or later.
- Ensure that you have PostgreSQL installed and running on your machine.
- Open the
appsettings.json
file and update the connection string to point to your PostgreSQL instance. - Run the project. The API will be hosted on
http://localhost:5000
.
The API documentation is generated using Swagger. To access the documentation, run the project and navigate to http://localhost:5000/swagger/index.html
in your web browser. This will display the Swagger UI, which provides documentation for all the API endpoints.