Skip to content

Commit

Permalink
feat(domain): add initial client example
Browse files Browse the repository at this point in the history
  • Loading branch information
MorveN11 committed Sep 8, 2024
1 parent 61ace68 commit 1128e35
Show file tree
Hide file tree
Showing 15 changed files with 215 additions and 23 deletions.
99 changes: 96 additions & 3 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,23 +45,37 @@ If you find a bug, please open an issue on GitHub and include:

With these steps, you have the project set up and ready to start development.

## Building all the Modules
## Commands for Development

### Building all the Modules

Build all the modules with the following command:

```bash
dotnet build
```

## Running the API
### Running the Database

```bash
pnpm start:db
```

### Reset the Database

```bash
pnpm db:restore
```

### Running the API

Run the API do it with the following command:

```bash
pnpm start:dev
```

## Testing
### Testing

Run the tests suite with the following command:

Expand All @@ -71,13 +85,88 @@ dotnet test

Add new tests to cover the changes you make.

### Open Coverage Report (Web)

First, you need to run the tests with the previous command and then you can open
the coverage report with the following command:

```bash
pnpm report
```

## Test Coverage

We aim to maintain a high level of test coverage to ensure the reliability and
stability of our codebase. When adding new features or making changes, please
ensure that you include the necessary tests to cover the changes. We want to
achieve a test coverage of at least 80% for our codebase.

## Docker Compose Services

Docker Compose is used to manage the services required for the project. The main
services are:

### PostgreSQL

PostgreSQL database is used to store the data for the API. The database is
created with the following configuration:

```yaml
db:
image: postgres:16.0
container_name: distribution-center-db
restart: always
environment:
POSTGRES_DB: distribution_center
POSTGRES_USER: ${POSTGRES_USER}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
TZ: 'UTC-4'
ports:
- 3030:3030
command: -p 3030
volumes:
- postgres_data:/var/lib/postgresql/data
- ./scripts/init.sql:/docker-entrypoint-initdb.d/1.sql
healthcheck:
test:
['CMD-SHELL', 'pg_isready -h localhost -p 3030 -d distribution_center']
interval: 5s
timeout: 5s
retries: 5
```

The initialization script is in the `scripts/init.sql` file.

You can connect to the database with the following url:
`postgresql://localhost:3030/distribution_center` and the credentials are in the
`.env` file.

### PgAdmin

PgAdmin is a web-based interface for managing the PostgreSQL database. PGAdmin
is created with the following configuration:

```yaml
admin:
image: dpage/pgadmin4:7.8
container_name: distribution-center-admin
restart: always
environment:
PGADMIN_DEFAULT_EMAIL: ${PGADMIN_EMAIL}
PGADMIN_DEFAULT_PASSWORD: ${PGADMIN_PASSWORD}
PGADMIN_LISTEN_PORT: 4040
ports:
- 4040:4040
volumes:
- pgadmin_data:/var/lib/pgadmin
depends_on:
db:
condition: service_healthy
```

You can access the PgAdmin interface at `http://localhost:4040` and the
credentials are in the `.env` file.

## Development Guidelines

- Follow the coding style and conventions outlined in the
Expand Down Expand Up @@ -159,3 +248,7 @@ under its [MIT License](LICENSE).

Thank you for your interest in contributing to this API! We look forward to your
contributions.

```
```
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@
"db:restore": "docker compose down && docker volume rm --force los-curiosos_postgres_data",
"start:db": "docker compose down && docker compose up -d",
"start:dev": "pnpm start:db && dotnet watch run --project ./src/DistributionCenter.Api",
"report:gen": "dotnet test && dotnet reportgenerator",
"report:show": "pnpm report:gen && xdg-open ./coverage/index.html",
"report": "dotnet reportgenerator && xdg-open ./coverage/index.html",
"prepare": "husky"
},
"devDependencies": {
Expand Down
2 changes: 0 additions & 2 deletions src/DistributionCenter.Commons/Errors/Error.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ namespace DistributionCenter.Commons.Errors;
public readonly record struct Error : IError
{
public string Code { get; }

public string Description { get; }

public ErrorType Type { get; }

private Error(string code, string description, ErrorType type)
Expand Down
2 changes: 0 additions & 2 deletions src/DistributionCenter.Commons/Errors/Interfaces/IError.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ namespace DistributionCenter.Commons.Errors.Interfaces;
public interface IError
{
string Code { get; }

string Description { get; }

ErrorType Type { get; }
}
3 changes: 0 additions & 3 deletions src/DistributionCenter.Commons/Results/Interfaces/IResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,12 @@ namespace DistributionCenter.Commons.Results.Interfaces;
public interface IResult
{
bool IsSuccess { get; }

ICollection<IError> Errors { get; }
}

public interface IResult<T> : IResult
{
T Value { get; }

TNext Match<TNext>(Func<T, TNext> success, Func<ICollection<IError>, TNext> failure);

Task<TNext> MatchAsync<TNext>(Func<T, Task<TNext>> success, Func<ICollection<IError>, Task<TNext>> failure);
}
1 change: 0 additions & 1 deletion src/DistributionCenter.Commons/Results/Result.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ protected Result(ICollection<IError> errors)
}

public bool IsSuccess => _errors is null || _errors.Count == 0;

public ICollection<IError> Errors
{
get
Expand Down
9 changes: 9 additions & 0 deletions src/DistributionCenter.Domain/Bases/BaseEntity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace DistributionCenter.Domain.Bases;

using DistributionCenter.Domain.Interfaces;

public abstract class BaseEntity : BaseRegister, IEntity
{
public Guid Id { get; init; } = Guid.NewGuid();
public bool IsActive { get; init; } = true;
}
9 changes: 9 additions & 0 deletions src/DistributionCenter.Domain/Bases/BaseRegister.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace DistributionCenter.Domain.Bases;

using DistributionCenter.Domain.Interfaces;

public abstract class BaseRegister : IRegister
{
public DateTime CreatedAt { get; init; } = DateTime.Now;
public DateTime? UpdatedAt { get; init; }
}
10 changes: 10 additions & 0 deletions src/DistributionCenter.Domain/Concretes/Client.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace DistributionCenter.Domain.Concretes;

using DistributionCenter.Domain.Bases;

public class Client : BaseEntity
{
public required string Name { get; init; }
public required string LastName { get; init; }
public required string PhoneNumber { get; init; }
}
7 changes: 7 additions & 0 deletions src/DistributionCenter.Domain/Interfaces/IEntity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace DistributionCenter.Domain.Interfaces;

public interface IEntity
{
Guid Id { get; }
bool IsActive { get; }
}
7 changes: 7 additions & 0 deletions src/DistributionCenter.Domain/Interfaces/IRegister.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace DistributionCenter.Domain.Interfaces;

public interface IRegister
{
DateTime CreatedAt { get; }
DateTime? UpdatedAt { get; }
}
10 changes: 0 additions & 10 deletions test/DistributionCenter.Domain.Tests/ATests.cs

This file was deleted.

22 changes: 22 additions & 0 deletions test/DistributionCenter.Domain.Tests/Bases/BaseEntityTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace DistributionCenter.Domain.Tests.Bases;

using DistributionCenter.Domain.Bases;

public class BaseEntityTests
{
[Fact]
public void Test_Base_Entity()
{
// Define Input and output
bool expectedIsActive = true;
Mock<BaseEntity> entityMock = new() { CallBase = true };

// Execute actual operation
Guid id = entityMock.Object.Id;
bool isActive = entityMock.Object.IsActive;

// Verify actual result
Assert.NotEqual(Guid.Empty, id);
Assert.Equal(expectedIsActive, isActive);
}
}
22 changes: 22 additions & 0 deletions test/DistributionCenter.Domain.Tests/Bases/BaseRegisterTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace DistributionCenter.Domain.Tests.Bases;

using DistributionCenter.Domain.Bases;

public class BaseRegisterTests
{
[Fact]
public void Test_Base_Register()
{
// Define Input and output
DateTime? expectedUpdatedAt = null;
Mock<BaseRegister> entityMock = new() { CallBase = true };

// Execute actual operation
DateTime createdAt = entityMock.Object.CreatedAt;
DateTime? updatedAt = entityMock.Object.UpdatedAt;

// Verify actual result
Assert.True(createdAt <= DateTime.Now);
Assert.Equal(expectedUpdatedAt, updatedAt);
}
}
32 changes: 32 additions & 0 deletions test/DistributionCenter.Domain.Tests/Concretes/ClientTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
namespace DistributionCenter.Domain.Tests.Concretes;

using DistributionCenter.Domain.Concretes;

public class ClientTests
{
[Fact]
public void Test_Client()
{
// Define Input and output
string expectedName = "Client Name";
string expectedLastName = "Client LastName";
string expectedPhoneNumber = "Client Phone Number";
Client entity =
new()
{
Name = expectedName,
LastName = expectedLastName,
PhoneNumber = expectedPhoneNumber,
};

// Execute actual operation
string name = entity.Name;
string lastName = entity.LastName;
string phoneNumber = entity.PhoneNumber;

// Verify actual result
Assert.Equal(expectedName, name);
Assert.Equal(expectedLastName, lastName);
Assert.Equal(expectedPhoneNumber, phoneNumber);
}
}

0 comments on commit 1128e35

Please sign in to comment.