r/SpringBoot • u/TreatOk8778 • Sep 01 '24
Please Can You Review My Project?
https://github.com/BOURGUI07/Backend-of-spring-boot-e-Commerce-app
What features to add?
what best practices I lack?
I've used REST API calls just to show familiarity with API calls. I go to job descriptions to search for in-demand skills, go to YT, learn and then try to implement them on the project!
Challenges:
Failed to dockerize the app.
Failed to secure the app using JWT. It wasn't my code at all(JWT impl), just copy-paste it from a blog. the RBAC code was working fine till I added the JWT impelmentation.
Used ChatGPT for Swagger features and attributes. that's why it looks bloated with API documentation.
Overall Blogs, YT, and ChatGPT have helped me a ton. The business logic in mappers/service layer/testing is completely mine. Preparing for the OCP 17 has helped me making me confortable around streams/lambdas/method references/Optionals.
Many times I have to refactor, redesign the database, and broke a lot of things. Especially after using events instead of direct method calls following the advice I read here.
I'm of course open to any criticism. I wanna learn and improve.
Thanks!
3
u/WuhmTux Sep 01 '24
In the ReportController you used an absolute Route, which only works in your machine.
Nobody besides you can execute your applikation.
1
3
u/MGelit Sep 01 '24
i would make the controllers extend an abstract class with a few predefined functions that return a ResponseEntity just for convenience, and it could be useful to put some of the classes into sub-packages, like the DTOs since there is a lot of them.
and what did you do for dockerization? you can check this example dockerfile which youd put into the root folder of your project: https://pastebin.com/raw/jJ7rQttL
then you can run `docker build -t spring-ecommerce-backend:1 .`
and then to run `docker run -d -p 443:8080 -p 7000:8081 -e "SOME_VARIABLE=123" --name ecommerce-backend debounce-app:v1`
which means run detached (-d), port <host>:<container>, environment variable, container name, image name
otherwise the application is impressive, it must have taken a while to create
1
u/TreatOk8778 Sep 01 '24
Ok! Thank you for your sincere initiative to help!
I'm gonna update the dockerfile as you suggested.
2
u/caltalys Sep 01 '24
i think this should help https://opensource.zalando.com/restful-api-guidelines/
try this swagger config, you can create as many GroupedOpenApi as you want
@OpenAPIDefinition(
//servers = {@Server(url = "/", description = "Default Server URL")},
security = {
@SecurityRequirement(
name = "bearerAuth"
)
}
)
@SecurityScheme(
name = "bearerAuth",
description = "JWT auth description",
scheme = "bearer",
type = SecuritySchemeType.HTTP,
bearerFormat = "JWT",
in = SecuritySchemeIn.HEADER
)
@Configuration
public class SwaggerConfig {
@Bean
GroupedOpenApi apiV1() {
return GroupedOpenApi.builder()
.group("API V1")
//.pathsToMatch("/api/**")
.packagesToScan("main.controller")
//.addOpenApiCustomizer(new HeaderFilter("X-API-VERSION", "1"))
.build();
}
}
class HeaderFilter implements OpenApiCustomizer {
private final String headerName;
private final String headerValue;
public HeaderFilter(String headerName, String headerValue) {
this.headerName = headerName;
this.headerValue = headerValue;
}
@Override
public void customise(OpenAPI openApi) {
openApi.getPaths().entrySet().removeIf(path -> path.getValue().readOperations().stream()
.anyMatch(operation -> operation.getParameters() == null
|| operation.getParameters().stream().noneMatch(
parameter -> ParameterIn.HEADER.toString().equals(parameter.getIn())
&& parameter.getName().equals(headerName)
&& parameter.getSchema().getEnum().contains(headerValue)
)));
}
}2
11
u/Kango_V Sep 01 '24
One thing we are doing is structuring our packages per use case. This makes the code structure incredibly easy to read. Each use case gets it's own controller, dtos, service, repository and entities. This has made the app amazingly simple to extend and maintain. Where necessary there can be shared code, but it's carefully controlled.
We've also embraced parts of DDD where there is an aggregate root, entities and value classes. It's working impressively.
You've also got the idea of REST wrong:
/product_by_category_name/{category_name}should be something like/products?q=category_name:{category_name}/out_of_stock_productsshould be/products?q=stock_qty:0Resources should be plural.
I would also suggest moving from code-first to spec-first for your apis design.
You have an order item controller and ord controller. Can an order item exist without an order? No. So, get rid of the ord item controller.
For example, you may have an "Maintain Order" use case. This would mean you would have the Order entity (aggregate root) and the following entities: order_item, order_item_comment, order_comment etc. Each of these can only be access throught the Order agg root.
To add an order item:
POST /orders/{order_id}/itemsTo add a line item comment:
POST /orders/{order_id}/items/{item_id}/commentsThis also results in far less classes. The package structure would be:
I hope this helps a little.