secure-files

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

DroppedFilesRepository.java (2804B)


      1 package xyz.kebigon.securefiles.db;
      2 
      3 import java.sql.Connection;
      4 import java.sql.PreparedStatement;
      5 import java.sql.ResultSet;
      6 import java.sql.SQLException;
      7 import java.util.ArrayList;
      8 import java.util.Collection;
      9 import java.util.Optional;
     10 
     11 import javax.sql.DataSource;
     12 
     13 import org.springframework.beans.factory.annotation.Autowired;
     14 import org.springframework.stereotype.Service;
     15 
     16 import lombok.extern.slf4j.Slf4j;
     17 
     18 @Slf4j
     19 @Service
     20 public class DroppedFilesRepository
     21 {
     22 	@Autowired
     23 	private DataSource dataSource;
     24 
     25 	public Iterable<DroppedFile> findAll() throws SQLException
     26 	{
     27 		try (Connection connection = dataSource.getConnection())
     28 		{
     29 			try (final PreparedStatement ps = connection.prepareStatement("SELECT * FROM `dropped_files`"))
     30 			{
     31 				final ResultSet rs = ps.executeQuery();
     32 
     33 				final Collection<DroppedFile> result = new ArrayList<>();
     34 				while (rs.next())
     35 					result.add(DroppedFile.mapToDroppedFile(rs));
     36 
     37 				log.info("findAll -> {}", result);
     38 				return result;
     39 			}
     40 		}
     41 		catch (final Throwable e)
     42 		{
     43 			log.error("findAll", e);
     44 			throw e;
     45 		}
     46 	}
     47 
     48 	public Optional<DroppedFile> findById(final String id) throws SQLException
     49 	{
     50 		try (Connection connection = dataSource.getConnection())
     51 		{
     52 			try (final PreparedStatement ps = connection.prepareStatement("SELECT * FROM `dropped_files` WHERE `id` = ? LIMIT 1"))
     53 			{
     54 				ps.setString(1, id);
     55 				final ResultSet rs = ps.executeQuery();
     56 
     57 				final Optional<DroppedFile> result = rs.next() ? Optional.of(DroppedFile.mapToDroppedFile(rs)) : Optional.empty();
     58 
     59 				log.info("findById: id={} -> {}", id, result);
     60 				return result;
     61 			}
     62 		}
     63 		catch (final Throwable e)
     64 		{
     65 			log.error("findById: id={}", id, e);
     66 			throw e;
     67 		}
     68 	}
     69 
     70 	public void save(final DroppedFile entity) throws SQLException
     71 	{
     72 		try (Connection connection = dataSource.getConnection())
     73 		{
     74 			try (final PreparedStatement ps = connection.prepareStatement("REPLACE INTO `dropped_files` VALUES(?, ?, ?, ?, ?)"))
     75 			{
     76 				ps.setString(1, entity.getId());
     77 				ps.setString(2, entity.getName());
     78 				ps.setString(3, entity.getContentType());
     79 				ps.setBoolean(4, entity.isDownloaded());
     80 				ps.setString(5, entity.getRemoteAddr());
     81 				ps.executeUpdate();
     82 
     83 				log.info("save: entity={}", entity);
     84 				return;
     85 			}
     86 		}
     87 		catch (final Throwable e)
     88 		{
     89 			log.error("save: entity={}", entity, e);
     90 			throw e;
     91 		}
     92 	}
     93 
     94 	public void deleteById(final String id) throws SQLException
     95 	{
     96 		try (Connection connection = dataSource.getConnection())
     97 		{
     98 			try (final PreparedStatement ps = connection.prepareStatement("DELETE FROM `dropped_files` WHERE `id` = ?"))
     99 			{
    100 				ps.setString(1, id);
    101 				ps.executeUpdate();
    102 
    103 				log.info("deleteById: id={}", id);
    104 				return;
    105 			}
    106 		}
    107 		catch (final Throwable e)
    108 		{
    109 			log.error("deleteById: id={}", id, e);
    110 			throw e;
    111 		}
    112 	}
    113 }