Commit 3f5012a4 authored by psakac's avatar psakac
Browse files

Added RobberyEventEndpoint

parent 2e90477a
......@@ -18,11 +18,12 @@ public class BankBranchEndpoint {
public BankBranch get(@PathVariable Long id) { return repository.getBankBranch(id); }
@GetMapping(value = "/")
public List<BankBranch> get() { return repository.getAllBankBranchs(); }
public List<BankBranch> getAll() { return repository.getAllBankBranchs(); }
@PostMapping(value = "/create")
public void create(@RequestBody BankBranch bankBranch) {
repository.createBankBranch(bankBranch); }
repository.createBankBranch(bankBranch);
}
@DeleteMapping(value = "/delete/{id}")
public boolean delete(@PathVariable Long id) { return repository.deleteBankBranch(id); }
......
package com.tuke.fei.atvi2.api.endpoint;
import com.tuke.fei.atvi2.api.entity.RobberyEvent;
import com.tuke.fei.atvi2.api.repository.RobberyEventRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("robbery-event")
public class RobberyEventEndpoint {
@Autowired
RobberyEventRepository repository;
@GetMapping("/all/{limit}")
public List<RobberyEvent> getAll(@PathVariable Integer limit) {
return repository.getAll(limit);
}
@GetMapping("/{branchId}/{limit}")
public List<RobberyEvent> getAllByBranchId(@PathVariable Long branchId, @PathVariable Integer limit) {
return repository.getAllByBranchId(branchId, limit);
}
@PostMapping(value = "/create")
public boolean create(@RequestBody RobberyEvent robberyEvent) {
return repository.create(robberyEvent);
}
}
package com.tuke.fei.atvi2.api.entity;
import java.sql.Timestamp;
public class RobberyEvent {
private Long id;
private BankBranch bankBranch;
private Timestamp timestamp;
private Integer rate;
private Integer audioMatch;
private Integer videoMatch;
private String audioUri;
private String videoUri;
private String keywords;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public BankBranch getBankBranch() {
return bankBranch;
}
public void setBankBranch(BankBranch bankBranch) {
this.bankBranch = bankBranch;
}
public Timestamp getTimestamp() {
return timestamp;
}
public void setTimestamp(Timestamp timestamp) {
this.timestamp = timestamp;
}
public Integer getRate() {
return rate;
}
public void setRate(Integer rate) {
this.rate = rate;
}
public Integer getAudioMatch() {
return audioMatch;
}
public void setAudioMatch(Integer audioMatch) {
this.audioMatch = audioMatch;
}
public Integer getVideoMatch() {
return videoMatch;
}
public void setVideoMatch(Integer videoMatch) {
this.videoMatch = videoMatch;
}
public String getAudioUri() {
return audioUri;
}
public void setAudioUri(String audioUri) {
this.audioUri = audioUri;
}
public String getVideoUri() {
return videoUri;
}
public void setVideoUri(String videoUri) {
this.videoUri = videoUri;
}
public String getKeywords() {
return keywords;
}
public void setKeywords(String keywords) {
this.keywords = keywords;
}
}
package com.tuke.fei.atvi2.api.repository;
import com.tuke.fei.atvi2.api.entity.BankBranch;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public class BankBranchRepository {
@Autowired
JdbcTemplate jdbcTemplate;
public interface BankBranchRepository {
public BankBranch getBankBranch(Long id) {
final String sql = "SELECT * FROM [bank_branch] WHERE id = ?";
try{
return (BankBranch) jdbcTemplate.queryForObject(sql, new Object[] {id}, new BeanPropertyRowMapper(BankBranch.class));
} catch (EmptyResultDataAccessException e) {
return null;
}
}
BankBranch getBankBranch(Long id);
public List<BankBranch> getAllBankBranchs() {
final String sql = "SELECT * FROM [bank_branch]";
return jdbcTemplate.query(sql, new BeanPropertyRowMapper(BankBranch.class));
}
List<BankBranch> getAllBankBranchs();
public void createBankBranch(BankBranch bankBranch) {
final String sql = "INSERT INTO [bank_branch] (address, zip, city) VALUES (?, ?, ?)";
jdbcTemplate.update(sql, bankBranch.getAddress(), bankBranch.getZip(), bankBranch.getCity());
}
void createBankBranch(BankBranch bankBranch);
public boolean deleteBankBranch(Long id) {
final String sql = "DELETE FROM [bank_branch] WHERE id = ?";
return jdbcTemplate.update(sql, id) == 1;
}
boolean deleteBankBranch(Long id);
}
package com.tuke.fei.atvi2.api.repository;
import com.tuke.fei.atvi2.api.entity.RobberyEvent;
import java.util.List;
public interface RobberyEventRepository {
List<RobberyEvent> getAll(Integer limit);
List<RobberyEvent> getAllByBranchId(Long branchId, Integer limit);
boolean create(RobberyEvent robberyEvent);
}
package com.tuke.fei.atvi2.api.repository.impl.sql;
import com.tuke.fei.atvi2.api.entity.BankBranch;
import com.tuke.fei.atvi2.api.repository.BankBranchRepository;
import com.tuke.fei.atvi2.api.repository.impl.sql.rowmapper.BankBranchRowMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public class SQLBankBranchRepository implements BankBranchRepository {
@Autowired
JdbcTemplate jdbcTemplate;
@Override
public BankBranch getBankBranch(Long id) {
final String sql = "SELECT " +
"[bank_branch].[id] AS [id_bb], " +
"[bank_branch].[address] AS [address_bb], " +
"[bank_branch].[city] AS [city_bb], " +
"[bank_branch].[zip] AS [zip_bb] " +
"FROM [bank_branch] WHERE id = ?";
try{
return jdbcTemplate.queryForObject(sql, new Object[] {id}, new BankBranchRowMapper());
} catch (EmptyResultDataAccessException e) {
return null;
}
}
@Override
public List<BankBranch> getAllBankBranchs() {
final String sql = "SELECT " +
"[bank_branch].[id] AS [id_bb], " +
"[bank_branch].[address] AS [address_bb], " +
"[bank_branch].[city] AS [city_bb], " +
"[bank_branch].[zip] AS [zip_bb] " +
"FROM [bank_branch]";
return jdbcTemplate.query(sql, new BankBranchRowMapper());
}
@Override
public void createBankBranch(BankBranch bankBranch) {
final String sql = "INSERT INTO [bank_branch] (address, zip, city) VALUES (?, ?, ?)";
jdbcTemplate.update(sql, bankBranch.getAddress(), bankBranch.getZip(), bankBranch.getCity());
}
@Override
public boolean deleteBankBranch(Long id) {
final String sql = "DELETE FROM [bank_branch] WHERE id = ?";
return jdbcTemplate.update(sql, id) == 1;
}
}
\ No newline at end of file
package com.tuke.fei.atvi2.api.repository.impl.sql;
import com.tuke.fei.atvi2.api.entity.RobberyEvent;
import com.tuke.fei.atvi2.api.repository.RobberyEventRepository;
import com.tuke.fei.atvi2.api.repository.impl.sql.rowmapper.RobberyEventRowMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public class SQLRobberyEventRepository implements RobberyEventRepository {
@Autowired
JdbcTemplate jdbcTemplate;
@Override
public List<RobberyEvent> getAll(Integer limit) {
final String sql = "SELECT TOP (?) " +
"[robbery_event].[id] AS [id_re], " +
"[robbery_event].[id_bank_branch] AS [id_bank_branch_re], " +
"[robbery_event].[timestamp] AS [timestamp_re], " +
"[robbery_event].[rate] AS [rate_re], " +
"[robbery_event].[audio_match] AS [audio_match_re], " +
"[robbery_event].[video_match] AS [video_match_re], " +
"[robbery_event].[audio_uri] AS [audio_uri_re], " +
"[robbery_event].[video_uri] AS [video_uri_re], " +
"[robbery_event].[keywords] AS [keywords_re], " +
"[bank_branch].[id] AS [id_bb], " +
"[bank_branch].[address] AS [address_bb], " +
"[bank_branch].[city] AS [city_bb], " +
"[bank_branch].[zip] AS [zip_bb] " +
"FROM [robbery_event] " +
"INNER JOIN [bank_branch] ON [robbery_event].[id_bank_branch] = [bank_branch].[id] " +
"ORDER BY [robbery_event].[timestamp]";
return jdbcTemplate.query(sql, new Object[] {limit}, new RobberyEventRowMapper());
}
@Override
public List<RobberyEvent> getAllByBranchId(Long branchId, Integer limit) {
final String sql = "SELECT TOP (?) " +
"[robbery_event].[id] AS [id_re], " +
"[robbery_event].[id_bank_branch] AS [id_bank_branch_re], " +
"[robbery_event].[timestamp] AS [timestamp_re], " +
"[robbery_event].[rate] AS [rate_re], " +
"[robbery_event].[audio_match] AS [audio_match_re], " +
"[robbery_event].[video_match] AS [video_match_re], " +
"[robbery_event].[audio_uri] AS [audio_uri_re], " +
"[robbery_event].[video_uri] AS [video_uri_re], " +
"[robbery_event].[keywords] AS [keywords_re], " +
"[bank_branch].[id] AS [id_bb], " +
"[bank_branch].[address] AS [address_bb], " +
"[bank_branch].[city] AS [city_bb], " +
"[bank_branch].[zip] AS [zip_bb] " +
"FROM [robbery_event] " +
"INNER JOIN [bank_branch] ON [robbery_event].[id_bank_branch] = [bank_branch].[id] " +
"WHERE [robbery_event].[id_bank_branch] = ? " +
"ORDER BY [robbery_event].[timestamp]";
return jdbcTemplate.query(sql, new Object[] {limit, branchId}, new RobberyEventRowMapper());
}
@Override
public boolean create(RobberyEvent robberyEvent) {
final String sql = "INSERT INTO [robbery_event] (id_bank_branch, rate, audio_match, video_match, " +
"audio_uri, video_uri, keywords) VALUES (?, ?, ?, ?, ?, ?, ?)";
return jdbcTemplate.update(sql, robberyEvent.getBankBranch().getId(),
robberyEvent.getRate(), robberyEvent.getAudioMatch(), robberyEvent.getVideoMatch(),
robberyEvent.getAudioUri(), robberyEvent.getVideoUri(), robberyEvent.getKeywords()) == 1;
}
}
package com.tuke.fei.atvi2.api.repository.impl.sql.rowmapper;
import com.tuke.fei.atvi2.api.entity.BankBranch;
import org.springframework.jdbc.core.RowMapper;
import java.sql.ResultSet;
import java.sql.SQLException;
public class BankBranchRowMapper implements RowMapper<BankBranch> {
@Override
public BankBranch mapRow(ResultSet rs, int i) throws SQLException {
BankBranch bankBranch = new BankBranch();
bankBranch.setId(rs.getLong("id_bb"));
bankBranch.setAddress(rs.getString("address_bb"));
bankBranch.setCity(rs.getString("city_bb"));
bankBranch.setZip(rs.getString("zip_bb"));
return bankBranch;
}
}
package com.tuke.fei.atvi2.api.repository.impl.sql.rowmapper;
import com.tuke.fei.atvi2.api.entity.RobberyEvent;
import org.springframework.jdbc.core.RowMapper;
import java.sql.ResultSet;
import java.sql.SQLException;
public class RobberyEventRowMapper implements RowMapper<RobberyEvent> {
@Override
public RobberyEvent mapRow(ResultSet rs, int i) throws SQLException {
RobberyEvent robberyEvent = new RobberyEvent();
robberyEvent.setId(rs.getLong("id_re"));
robberyEvent.setAudioMatch(rs.getInt("audio_match_re"));
robberyEvent.setAudioUri(rs.getString("audio_uri_re"));
robberyEvent.setBankBranch(new BankBranchRowMapper().mapRow(rs, i));
robberyEvent.setKeywords(rs.getString("keywords_re"));
robberyEvent.setRate(rs.getInt("rate_re"));
robberyEvent.setTimestamp(rs.getTimestamp("timestamp_re"));
robberyEvent.setVideoMatch(rs.getInt("video_match_re"));
robberyEvent.setVideoUri(rs.getString("video_uri_re"));
return robberyEvent;
}
}
......@@ -4,3 +4,24 @@ CREATE TABLE [data] (
phrase VARCHAR(64) NOT NULL,
bank_robbery DECIMAL(5,2) NULL,
PRIMARY KEY (id));
CREATE TABLE [bank_branch] (
id INT IDENTITY(1,1) NOT NULL,
address VARCHAR(64) NOT NULL,
zip VARCHAR(64) NOT NULL,
city VARCHAR(64) NOT NULL,
PRIMARY KEY (id));
CREATE TABLE [robbery_event] (
id INT IDENTITY(1,1) NOT NULL,
id_bank_branch INT NOT NULL,
timestamp DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
rate INT NOT NULL,
audio_match INT NOT NULL,
video_match INT NOT NULL,
audio_uri VARCHAR(128) NULL,
video_uri VARCHAR(128) NULL,
keywords VARCHAR(256) NULL,
PRIMARY KEY (id),
FOREIGN KEY (id_bank_branch) REFERENCES bank_branch(id)
);
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment