https://github.com/pandatecham/be-lib-service-response
This is micro library developed by PandaTech to standardize API responses.
https://github.com/pandatecham/be-lib-service-response
api pandatech
Last synced: 7 months ago
JSON representation
This is micro library developed by PandaTech to standardize API responses.
- Host: GitHub
- URL: https://github.com/pandatecham/be-lib-service-response
- Owner: PandaTechAM
- License: mit
- Created: 2023-02-14T20:19:49.000Z (almost 3 years ago)
- Default Branch: development
- Last Pushed: 2024-11-23T07:14:13.000Z (about 1 year ago)
- Last Synced: 2025-03-18T19:30:30.176Z (10 months ago)
- Topics: api, pandatech
- Language: C#
- Homepage:
- Size: 241 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# ServiceResponse
## Intro
This is service response template for .Net 6+ web api projects. This template is based on the best practices and
has goal to harmonize all API I/O operations. The benefit of this template over other templates is that it totally
integrates with OpenAPI and Swagger. So, IActionResults and other services,
response will be visible in Swagger UI and loads of other features are and is going to be included.
This Template is designed by PandaTech LLC.
We build software with the greatest quality!
Our website: www.pandatech.it :)
---
## Example
### Model
```cs
public class Blog
{
public int BlogId { get; set; }
public string BlogName { get; set; }
public string BlogDescription { get; set; }
}
public class Post
{
public int PostId { get; set; }
public string PostName { get; set; }
public string PostDescription { get; set; }
public int BlogId { get; set; }
}
```
### Program.cs extract
```cs
builder.Services.AddTransient();
#if DEBUG
builder.Services.AddTransient();
#else
builder.Services.AddTransient();
#endif
```
### Service
```cs
public class Services : IService
{
public ServiceResponse DeletePost(int postId)
{
var serviceResponse = new ServiceResponse();
if (postId == 0) // just for example, cannot be in real case
{
serviceResponse.ResponseStatus = ServiceResponseStatus.NotFound;
serviceResponse.Message = $"Post with id {postId} not found";
serviceResponse.Success = false;
}
else
{
serviceResponse.Message = $"Post with id {postId} deleted";
serviceResponse.ResponseStatus = ServiceResponseStatus.Ok;
}
return serviceResponse;
}
public ServiceResponse GetPost(int postId)
{
var serviceResponse = new ServiceResponse();
serviceResponse.Data = new Post
{
PostId = postId,
PostName = "Your post name",
PostDescription = "Post Description",
};
return serviceResponse;
}
public ServiceResponsePaged> GetAllBlogs(int page, int pageSize)
{
var serviceResponse = new ServiceResponsePaged>();
serviceResponse.Page = page;
serviceResponse.PageSize = pageSize;
serviceResponse.TotalCount = 1; // just for example
serviceResponse.Data = new List
{
new Blog
{
BlogId = 1,
BlogName = "Your blog name",
BlogDescription = "Blog Description",
}
};
return serviceResponse;
}
}
```
### Controller
```cs
[ApiController]
[Route("[controller]")]
public class DemoController : ExtendedController
{
private IService _service;
public DemoController(IExceptionHandler exceptionHandler, IService service) : base(exceptionHandler)
{
_service = service;
}
[HttpDelete("Post")]
public ServiceResponse DeletePost(int postId)
{
try
{
return _service.DeletePost(postId);
}
catch (Exception e)
{
return ExceptionHandler.Handle(new ServiceResponse(), e);
}
}
[HttpGet("Post")]
public ServiceResponse GetPost(int postId)
{
try
{
return _service.GetPost(int postId);
}
catch (Exception e)
{
return ExceptionHandler.Handle(new ServiceResponse(), e);
}
}
[HttpGet("Blogs")]
public ServiceResponsePaged> GetAllBlogs(int page, int pageSize)
{
try
{
return _service.GetAllBlogs(page,pageSize);
}
catch (Exception e)
{
return ExceptionHandler.Handle(new ServiceResponsePaged(), e);
}
}
}
```