HiveBrain v1.2.0
Get Started
← Back to all entries
debugcppMinor

Mini eCommerce application and message/warning/error-handling

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
handlingerrorapplicationmessageminiecommerceandwarning

Problem

I decided to build a basic eCommerce application in c++ as a learning experience. In the past, I've gone with a fat controller, skinny model approach, and while it has worked, my results always end up messy and the code is/was kind of individual to the applications. To try something new, I wanted to incorporate the single responsibility principle (as much as possible) and group my application into service layers.

My current code can be found on Github.

Continuing with the actual architecture:

Entities: Base data-holder class.

Services: Handle all operations for a particular entity. Building, creating, updating, finding, and more.

Validators: Check entity data to ensure it meets any requirements. Interface is currently a single method, Validate(void * v). Implementation casts the passed in pointer to the appropriate entity.

int main(int argc, char ** argv)
{
    Pluto::Controller::AddSingleProduct * controllerAddSingleProduct = new Pluto::Controller::AddSingleProduct();
    controllerAddSingleProduct->ProcessRequest();

    return 0;
}


The ProcessRequest method called above:

void Pluto::Controller::AddSingleProduct::ProcessRequest()
{
    unsigned int productId = 1;
    std::string productName = "Test product.";
    unsigned int productPrice = 10;
    unsigned int productQuantity = 10;
    unsigned int productType = 1;
    unsigned int productStatus = 1;

    Pluto::Service::Product * productService = new Pluto::Service::Product();
    productService->SetProductValidator(new Pluto::Validator::Product());

    if (productService->Create(productService->Build(productId, productName, productPrice, productQuantity, productType, productStatus)))
    {
        this->ErrorView();
        return;
    }

    this->SuccessView();
}


The Pluto::Service::Product methods called above:

```
Pluto::Entity::Product * Pluto::Service::Product::Build(unsigned int id, std::string name, unsigned int price, unsigned int quantity, unsigned int type, unsigned int

Solution

I would agree that what you have now is cleaner and more testable (in comparison to the code you said you might have written before).

I don't know C++ but I'm familiar with validation, so I'll stick to that.

-
When adding a Product, I think it's best not to explicity set an ID. I'm assuming these entries are going to a database of some sorts (relational, key, or anything else). If this is the case, let the database set the ID, or let it tell you what ID comes up next.

-
I think it's limiting to have restrictions on name length. I could see an eCommerce store selling an ax. I'm not sure why you'd limit one and two letter words (why not three or four letter words);just check if there's a name at all.

-
Similar principle with the price. Supposedly it can't be less than 1. What if the store offers a holiday sale and the $1 item is 50% off, and the $0.25 is free! I'm just thinking of all the possible ways you could run into problems down the road.

-
Also, you set price as an int. This is fine, except now there's no cents. I suggest another variable handling the cents (also an int, limited to two digits).

Context

StackExchange Code Review Q#61607, answer score: 3

Revisions (0)

No revisions yet.