Git Product home page Git Product logo

booking-system's Introduction

Booking System

include

project Structure

There is two important folder, src which is the source code and rest api testing files for vscode-rest extension.

  • src
    • middleware - collection of middlewares
    • model - collection of mongoose model, also include error-safe wrapper functions for CRUD operations.
    • routers - collection of express routers
    • lib - collection of utility
      • errors.ts - custom define errors to create error-safe api
      • logger.ts - centralize logger to be easier to replace with logger service in the long run.
      • mock.ts - mock APIs, such as payment...
      • parsers.ts - collection of validation functions. Yes, it parse, inspired from Parse, Don't validate
      • schemas.ts - zod schemas to validate user inputs.

Design Approach

One of the most notable package is effect, which unlock the type safty in error handling.

I carefully implement the features in the mind of data-flow. The data is wrapped inside the type Effect, which is similar to Either in Haskell or Result<T, E> in rust with extra information.

For example, in packages.router.ts.

const task = pipe(
    req.params.packageId,
    getPackage(packages),
    Effect.tap((pkg) => paymentCharge({ price: pkg.price })),
    Effect.flatMap(createPackage),
    Effect.flatMap((pkg) => updateOneByEmail(req.email, { $push: { packages: pkg.id! } }, populate))
);
  1. When the user subscribe a package, the user going to send the id of the package.
  2. Then we query the currently available packages.
  3. Then make the payment
  4. Then store the subscribed package to the db.
  5. Update the user that he/she subscribed the package. (update the relation)

When run the task, the output will be, the user who successfully subscribed the package or some expected errors (subscribe not exist package, payment fail or error while updating database.)

const main = Effect.match(task, {
    onSuccess: (user) => {
        res.json({ status: "success", user });
    },
    onFailure: (e) => {
        switch (e._tag) {
            case "NotExistError":
                 res.status(404).json({ message: e.message });
                 break;
            case "PaymentError":
            case "DatabaseError":
                 res.status(500).json({ message: e.message });
                 break;
        }
    },
});

We can easily response the user with appropriate response.

booking-system's People

Contributors

oungseik avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.