Skip to content

Commit aad4c0c

Browse files
committed
MVP
0 parents  commit aad4c0c

26 files changed

+747
-0
lines changed

.github/bump.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/bin/bash
2+
3+
current_build=$(grep -o "<Version>0.1.\(.*\)<\/Version>" ./src/Initium/Initium.csproj | sed 's/<Version>0.1.\(.*\)<\/Version>/\1/')
4+
new_build=$(printf "%d" $((10#$current_build + 1)))
5+
sed -i "s/<Version>0.1.$current_build<\/Version>/<Version>0.1.$new_build<\/Version>/" ./src/Initium/Initium.csproj
6+
7+
# Write the new build to the environment file
8+
echo "new_build=0.1.$new_build" >> $GITHUB_ENV
9+
10+
echo "Version updated to 0.1.$new_build"

.github/workflows/publish.yml

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
name: Publish NuGet
2+
3+
on:
4+
push:
5+
branches: [ "master" ]
6+
7+
jobs:
8+
build:
9+
runs-on: ubuntu-latest
10+
permissions:
11+
contents: write
12+
packages: write
13+
14+
steps:
15+
- uses: actions/checkout@v4
16+
17+
- name: Setup .NET
18+
uses: actions/setup-dotnet@v4
19+
with:
20+
dotnet-version: 8.0.x
21+
22+
- name: Build
23+
run: dotnet build ./src/Initium/Initium.csproj --configuration Release
24+
25+
- name: Bump Version
26+
run: |
27+
chmod +x "${GITHUB_WORKSPACE}/.github/bump.sh"
28+
"${GITHUB_WORKSPACE}/.github/bump.sh"
29+
30+
- name: Pack NuGet Package
31+
run: dotnet pack ./src/Initium/Initium.csproj --configuration Release --output ./nupkg
32+
33+
- name: Push to NuGet
34+
env:
35+
NUGET_API_KEY: ${{ secrets.NUGET_API_KEY }}
36+
run: dotnet nuget push "./nupkg/*.nupkg" --api-key $NUGET_API_KEY --source https://api.nuget.org/v3/index.json --skip-duplicate
37+
38+
- name: Push to GitHub NuGet
39+
env:
40+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
41+
run: dotnet nuget push "./nupkg/*.nupkg" --api-key $GITHUB_TOKEN --source "https://nuget.pkg.github.com/imclint21/index.json" --skip-duplicate
42+
43+
- name: Remove Packages
44+
run: rm -rf *.nupkg ./nupkg
45+
46+
- uses: stefanzweifel/git-auto-commit-action@v5
47+
with:
48+
commit_message: Bump Version

.gitignore

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
.idea
2+
.vs
3+
.vscode
4+
5+
**/bin
6+
**/obj
7+
8+
**/.DS_Store
9+
10+
**/#Deprecated
11+
12+
*.sln
13+
14+
**/Demo
15+
16+
**/*.nupkg

CONTRIBUTING.md

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# Contributing to Initium
2+
3+
Thank you for your interest in contributing to **Initium**! We value contributions from the community, whether it's fixing bugs, adding features, improving documentation, or suggesting new ideas. Please follow these guidelines to ensure a smooth collaboration process.
4+
5+
---
6+
7+
## Getting Started
8+
9+
### 1. Fork the Repository
10+
- Navigate to the [Initium repository](https://github.com/imclint21/Initium).
11+
- Click the **Fork** button in the top-right corner to create a copy of the repository in your GitHub account.
12+
13+
### 2. Clone Your Fork
14+
```bash
15+
git clone https://github.com/yourusername/Initium.git
16+
```
17+
Replace `yourusername` with your GitHub username.
18+
19+
### 3. Set Up the Development Environment
20+
- Ensure you have the following installed:
21+
- .NET 6 SDK or higher
22+
- Git
23+
- Navigate to the project folder and restore dependencies:
24+
```bash
25+
cd Initium
26+
dotnet restore
27+
```
28+
29+
### 4. Create a New Branch
30+
Use the following branch naming convention for new features:
31+
```bash
32+
git checkout -b features/<feature-name>
33+
```
34+
Replace `<feature-name>` with a brief description of the feature or fix.
35+
36+
---
37+
38+
## Making Changes
39+
40+
### Code Guidelines
41+
- Follow the project's existing code style and organization.
42+
- Include comments to explain complex code and logic.
43+
- Write unit tests for any new features or bug fixes.
44+
45+
### Running Tests
46+
Run the test suite to ensure your changes don't break existing functionality:
47+
```bash
48+
dotnet test
49+
```
50+
51+
---
52+
53+
## Submitting Your Contribution
54+
55+
### 1. Commit Your Changes
56+
Use clear and concise commit messages:
57+
```bash
58+
git add .
59+
git commit -m "Add feature: <feature-name>"
60+
```
61+
62+
### 2. Push to Your Fork
63+
Push your changes to the branch you created:
64+
```bash
65+
git push origin features/<feature-name>
66+
```
67+
68+
### 3. Open a Pull Request
69+
- Go to your fork on GitHub.
70+
- Click the **New Pull Request** button.
71+
- Select your branch and compare it with the `main` branch of the original repository.
72+
- Provide a descriptive title and summary of your changes.
73+
74+
75+
## Questions or Suggestions?
76+
77+
If you have any questions or suggestions, feel free to open an issue or reach out via GitHub discussions. We appreciate your feedback and contributions!
78+
79+
Thank you for helping to make Initium better!

LICENSE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2024 clint21.eth ⚡️
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Initium — Service Core Foundation
2+
3+
[![Publish NuGet](https://github.com/imclint21/Initium/actions/workflows/publish.yml/badge.svg?branch=master)](https://github.com/imclint21/Initium/actions/workflows/publish.yml)
4+
![DotNet](https://img.shields.io/badge/.NET-8.0%20LTS-blue)
5+
![MIT License](https://img.shields.io/badge/license-MIT-lightgrey.svg)
6+
7+
## Introduction
8+
9+
Initium is a library for simplifying .NET API development, offering standardized service operations, flexible routing, and seamless chaining of service results for cleaner and more maintainable code.
10+
11+
- **Streamlined API Controllers**: Simplifies response handling with attributes like `[ApiResponse]`, providing clear and consistent HTTP status documentation.
12+
- **Centralized Result Management**: `ServiceResult` enables clear success or failure status, making error handling and conditional logic seamless.
13+
- **Exception Handling Made Easy**: `ApiException` provides a straightforward way to handle specific HTTP error codes, improving code clarity.
14+
- **Result Chaining**: Methods return `ServiceResult` or typed results, allowing for intuitive chaining and cleaner service logic.
15+
- **Enhanced Maintainability**: Standardized patterns reduce boilerplate code, making APIs easier to build, understand, and maintain.
16+
17+
## Getting Started
18+
19+
To get started with Initium, just add the package using NuGet:
20+
21+
```bash
22+
dotnet add package Initium
23+
```
24+
25+
## How it Works?
26+
27+
Here's how to create a controller :
28+
29+
```csharp
30+
public class CoffeeController(CoffeeService service) : ApiController
31+
{
32+
[HttpPost]
33+
[ApiResponse(200, "Coffee prepared successfully.")]
34+
[ApiResponse(400, "An error occurred during the preparation process.")]
35+
public ActionResult PrepareCoffee() => service.PrepareCoffee();
36+
}
37+
```
38+
And here's how to create an action in a service, each function returns a `ServiceResult`, and can be chained.
39+
40+
```csharp
41+
public class CoffeeService
42+
{
43+
public ServiceResult DoSomething()
44+
{
45+
return ServiceResult.Error("Something happened!", HttpStatusCode.Conflict);
46+
}
47+
48+
public ServiceResult PrepareCoffee()
49+
{
50+
var doSomethingResult = DoSomething();
51+
if (doSomethingResult == false) return doSomethingResult;
52+
53+
return ServiceResult.Ok("The coffee is now DONE!");
54+
}
55+
}
56+
```
57+
58+
## Contribute to Initium
59+
60+
See [CONTRIBUTING.md](CONTRIBUTING.md) for best practices and instructions on setting up your development environment to work on Initium.

src/Initium/ApiController.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using Microsoft.AspNetCore.Mvc;
2+
using Initium.Filters;
3+
using Initium.Infrastructure;
4+
5+
namespace Initium;
6+
7+
[ApiController]
8+
[Route("api/[controller]")]
9+
[TypeFilter(typeof(ApiExceptionFilter))]
10+
[TypeFilter(typeof(ApiResponseFilter))]
11+
public class ApiController : BaseController;
12+
13+
public class ApiController<TService>(TService service) : ApiController where TService : class
14+
{
15+
public TService Service { get; } = service;
16+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System.Diagnostics.CodeAnalysis;
2+
using System.Net;
3+
4+
namespace Initium.Attributes;
5+
6+
[SuppressMessage("ReSharper", "UnusedMember.Global")]
7+
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, AllowMultiple = true)]
8+
public class ApiResponseAttribute : Attribute
9+
{
10+
public int StatusCode { get; }
11+
public string Message { get; }
12+
13+
public ApiResponseAttribute(HttpStatusCode statusCode, string message)
14+
{
15+
StatusCode = (int)statusCode;
16+
Message = message;
17+
}
18+
19+
public ApiResponseAttribute(int statusCode, string message)
20+
{
21+
StatusCode = statusCode;
22+
Message = message;
23+
}
24+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System.Net;
2+
3+
namespace Initium.Exceptions;
4+
5+
/// <summary>
6+
/// Represents an exception specific to API behavior with additional context like HTTP status code and custom messages.
7+
/// </summary>
8+
public class ApiException : Exception
9+
{
10+
/// <summary>
11+
/// Gets the HTTP status code associated with this exception.
12+
/// </summary>
13+
public HttpStatusCode StatusCode { get; }
14+
15+
/// <summary>
16+
/// Initializes a new instance of the <see cref="ApiException"/> class with a specified HTTP status code.
17+
/// </summary>
18+
/// <param name="statusCode">The HTTP status code associated with this exception.</param>
19+
public ApiException(HttpStatusCode statusCode)
20+
{
21+
StatusCode = statusCode;
22+
}
23+
24+
/// <summary>
25+
/// Initializes a new instance of the <see cref="ApiException"/> class with a specified HTTP status code and message.
26+
/// </summary>
27+
/// <param name="statusCode">The HTTP status code associated with this exception.</param>
28+
/// <param name="message">The message describing the error.</param>
29+
public ApiException(HttpStatusCode statusCode, string message) : base(message)
30+
{
31+
StatusCode = statusCode;
32+
}
33+
34+
/// <summary>
35+
/// Initializes a new instance of the <see cref="ApiException"/> class with a specified HTTP status code, message, and inner exception.
36+
/// </summary>
37+
/// <param name="statusCode">The HTTP status code associated with this exception.</param>
38+
/// <param name="message">The message describing the error.</param>
39+
/// <param name="innerException">The inner exception that caused the current exception.</param>
40+
public ApiException(HttpStatusCode statusCode, string message, Exception? innerException) : base(message, innerException)
41+
{
42+
StatusCode = statusCode;
43+
}
44+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
namespace Initium.Extensions;
2+
3+
/// <summary>
4+
/// Provides extension methods for working with <see cref="IEnumerable{T}"/> of strings.
5+
/// </summary>
6+
public static class EnumerableExtensions
7+
{
8+
/// <summary>
9+
/// Concatenates the elements of a sequence of strings, using the specified separator between each element.
10+
/// </summary>
11+
/// <param name="value">The sequence of strings to join.</param>
12+
/// <param name="separator">The string to use as a separator. The default is a single space (" ").</param>
13+
/// <returns>A single string that consists of the elements in the sequence, delimited by the separator.</returns>
14+
public static string Join(this IEnumerable<string> value, string separator = " ") =>
15+
string.Join(separator, value);
16+
}

0 commit comments

Comments
 (0)