Commit 0c039dd1 authored by Ondra's avatar Ondra
Browse files

fix: data init

parent cacc8226
Loading
Loading
Loading
Loading
+20 −20
Original line number Diff line number Diff line
@@ -7,21 +7,22 @@ import cz.muni.fi.gitlab.xbrodeck.tennis_api.storage.ownRepository.CourtSurfaceR
import lombok.RequiredArgsConstructor;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

@Configuration
@Component
@RequiredArgsConstructor
@ConditionalOnProperty(name = "init-data", havingValue = "true")
public class DataInitConfig {
    private final CourtSurfaceRepository courtSurfaceRepository;
    private final CourtRepository courtRepository;

    @Bean
    CommandLineRunner initData() {
    @Transactional
    @EventListener(ApplicationReadyEvent.class)
    void initData() {


        return args -> {
        CourtSurface clay = new CourtSurface("Clay", 50);
        CourtSurface grass = new CourtSurface("Grass", 100);

@@ -37,6 +38,5 @@ public class DataInitConfig {
        courtRepository.save(kidsCourt);
        courtRepository.save(trainingCourt);
        courtRepository.save(standardCourt);
        };
    }
}
+8 −1
Original line number Diff line number Diff line
@@ -41,8 +41,15 @@ public class Reservation extends GeneralEntity {
        this.singles = singles;
    }

    /**
     * Calculates the duration of the reservation in minutes.
     *
     * @return rounds up the duration in seconds to the nearest minute
     */
    private long getDurationInMinutes() {
        return java.time.Duration.between(startTime, endTime).toMinutes();
        long seconds = java.time.Duration.between(startTime, endTime).getSeconds();

        return (seconds + 59) / 60;
    }

    /**
+7 −1
Original line number Diff line number Diff line
@@ -3,6 +3,7 @@ package cz.muni.fi.gitlab.xbrodeck.tennis_api.storage.ownRepository;
import cz.muni.fi.gitlab.xbrodeck.tennis_api.business.model.GeneralEntity;
import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;
@@ -39,7 +40,12 @@ public class UniversalRepositoryImpl<Entity extends GeneralEntity, Key> implemen
    }

    @Override
    @Transactional
    public void deleteById(Key id) {
        entityManager.remove(entityManager.find(classType, id));
        Entity entity = entityManager.find(classType, id);
        if (entity != null) {
            entity.setDeleted(true);
            entityManager.merge(entity);
        }
    }
}