Code Monkey home page Code Monkey logo

barrage-java-study-fj's Introduction

**# Entertainment event project

Task 1

Let's begin our Java story :)

Let's imagine that you are a computer science student. You have several friends who are part of rock bands and love playing their music. However, they lack opportunities to monetize their creativity. They need a system to keep track of the participants at their concerts, plan their performances, announce future events, and so on.

What to do?

Task 2

Next we will continue learning Spring Framework and Java. Today we will have a new task is to connect PostgreSQL database to the project. In the project is available file docker-compose.yml with the described PostgreSQL database service.

In Spring Framework to work with the database created sub-module data-jpa, which implements Java Persistence API.

When working with the database it is necessary to take care of the database schema management. We will use the liquibase tool. In summary, for convenient work with the database we need to connect the following packages:

  1. Spring Data JPA
  2. Liquibase
  3. PostgreSQL driver

Additional information: https://www.baeldung.com/the-persistence-layer-with-spring-data-jpa https://docs.spring.io/spring-data/jpa/reference/jpa/getting-started.html https://www.baeldung.com/liquibase-refactor-schema-of-java-app https://docs.liquibase.com/start/home.html

Add the dependencies to our project:

  implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
  runtimeOnly 'org.postgresql:postgresql:42.7.1'
  implementation 'org.liquibase:liquibase-core:4.25.1'
Note that for `spring-boot-starter` packages it is not necessary to specify the package version, as the appropriate version of the gradle library will be selected by the `io.spring.dependency-management` plugin.
```
plugins {
  id 'java'
  id 'org.springframework.boot' version '3.2.2'
  id 'io.spring.dependency-management' version '1.1.4' // <-- here
}
```

Now it is necessary to describe the liquibase configuration and specify the database connection configuration.

LiquibaseConfiguration

@Configuration
public class LiquibaseConfiguration {
    @Bean
    public SpringLiquibase liquibase(DataSource dataSource) {
        SpringLiquibase liquibase = new SpringLiquibase();
        liquibase.setChangeLog("classpath:liquibase/master.xml");
        liquibase.setDataSource(dataSource);
        return liquibase;
    }
}

Data Scheme:

Migration index file classpath:liquibase/master.xml:

<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.4.xsd">
    <include file="classpath:liquibase/changelog/00000000000000_initial_schema.xml" />
</databaseChangeLog>

And the first migration:

<databaseChangeLog
        xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
        xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.4.xsd http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd">
    <changeSet id="20240126-1" author="alex.glebov">
....
    </changeSet>
</databaseChangeLog>

DB connection configs at application.yml:

spring:
  datasource:
    driver-class-name: org.postgresql.Driver
    url: jdbc:postgresql://localhost:5432/event
    username: event_user
    password: pa55w0rd

And we are ready to start our application.

What to do?

Practical exercise: Describe a CRUD to handle the Event entity, i.e. read, create, update, delete operations. Update the service code, and controller methods for all the above operations.

Task3

In the next task we need to add business logic for event tickets. Events can take place on different days, as well as repeat, for example, Cirque du Soleil, therefore we need to organise the storage of the schedule of events, as well as the number of available seats at the event. So, to create the schedule, let's create the event_schedule table

create table event_schedule (
  id serial primary key,
  event_id int not null,
  event_date date not null,
  available_seats int not null,
  price decimal(10,2) not null
);

table for selling tickets ticket_order

create table ticket_order (
  id serial primary key,
  firstname varchar(255) not null,
  lastname varchar(255) not null,
  email varchar(255) not null,
  amount int not null
);

Let's describe the entities for tables and JPA repositories:

...

Then we need to implement business logic to manage these entities (CRUD). Create CRUD endpoints.

In business logic sometimes exceptional situations occur, to handle them correctly we can use Spring Boot tools for exception handling. In exceptional situations we will throw exceptions and use Spring Boot tools to convert them into client-understandable DTOs and error codes.

Create your own exception types and use the @ControllerAdvice component to handle exceptions in your application, for example, when an object is not found in a data source.

Note that the number of ticket sales cannot be more than event_schedule.available_seats. To avoid this, you need to create an insert trigger, which will check the available quantity before inserting a new row. Do exception handling for the current case.

Think why this task should not be solved at the application level?

Task 4

concurrency and multi-threading logging + ELK testing: JUnit5, SpringBootTest

TBD ....

Task 5

TBD ....

Task 6

TBD ....

Task 7

TBD ....


Misc

Don't forget to create custom docker network to avoid any inconvenient circumstances with other docker services and keep current environment as isolated from the other projects

I can suggest to create

docker network create -d bridge --subnet 172.21.0.0/24 --gateway 172.21.0.1 dockernet

barrage-java-study-fj's People

Contributors

stormrider1996 avatar alexeyglebovsetronica 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.