Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

This file was deleted.

This file was deleted.

29 changes: 29 additions & 0 deletions kotiki-java/cats/core/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>cats</artifactId>
<groupId>ru.itmo.kotiki.cats</groupId>
<version>1.0-SNAPSHOT</version>
</parent>

<modelVersion>4.0.0</modelVersion>
<groupId>ru.itmo.kotiki.cats.core</groupId>
<artifactId>cats-core</artifactId>
<packaging>pom</packaging>

<dependencies>
<dependency>
<groupId>ru.itmo.kotiki.cats.data</groupId>
<artifactId>cats-data</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit</artifactId>
<version>2.4.4</version>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package ru.itmo.kotiki.cats.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitConfig {

@Bean
public DirectExchange catsDirectExchange() {
return new DirectExchange("cats-exchange");
}

@Bean
public Queue saveCat() {
return new Queue("saveCatQueue");
}

@Bean
public Queue deleteCatById() {
return new Queue("deleteByIdQueue");
}

@Bean
public Queue findCatById() {
return new Queue("findByIdQueue");
}

@Bean
public Queue findAllCatsByOwner() {
return new Queue("findAllByOwnerQueue");
}

@Bean
public Binding bindSaveCat() {
return BindingBuilder.bind(saveCat()).to(catsDirectExchange()).with("saveCat");
}

@Bean
public Binding bindDeleteCatById() {
return BindingBuilder.bind(deleteCatById()).to(catsDirectExchange()).with("deleteCatById");
}

@Bean
public Binding bindFindCatById() {
return BindingBuilder.bind(findCatById()).to(catsDirectExchange()).with("findCatById");
}

@Bean
public Binding bindFindAllCatsByOwner() {
return BindingBuilder.bind(findAllCatsByOwner()).to(catsDirectExchange()).with("findAllCatsByOwner");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package ru.itmo.kotiki.cats.service;

import java.util.List;
import ru.itmo.kotiki.dto.CatDto;
import ru.itmo.kotiki.entity.CatDtoOwnerId;
import ru.itmo.kotiki.entity.OwnerIdCatId;
import ru.itmo.kotiki.exception.ValidationException;

public interface CatService {

CatDto save(CatDtoOwnerId catDto) throws ValidationException;

void deleteById(OwnerIdCatId ids);

CatDto findById(OwnerIdCatId ids);

List<CatDto> findAllByOwner(int ownerId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package ru.itmo.kotiki.cats.service;

import static java.util.Objects.isNull;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import ru.itmo.kotiki.dto.CatDto;
import ru.itmo.kotiki.dto.OwnerDto;
import ru.itmo.kotiki.entity.CatDtoOwnerId;
import ru.itmo.kotiki.entity.CatOwnerId;
import ru.itmo.kotiki.entity.OwnerIdCatId;
import ru.itmo.kotiki.model.Cat;
import ru.itmo.kotiki.model.accessory.Color;
import ru.itmo.kotiki.exception.ValidationException;
import ru.itmo.kotiki.cats.repository.CatRepository;

@EnableRabbit
@Service
public class CatServiceImpl implements CatService {

@Autowired
private CatRepository catRepository;

@Autowired
private RabbitTemplate rabbitTemplate;

@Override
@RabbitListener(queues = "saveCatQueue")
public CatDto save(CatDtoOwnerId catDto) throws ValidationException {
validateCatDto(catDto.getCatDto());
Cat cat = catDto.getCatDto().toCat();
rabbitTemplate.convertSendAndReceive("addCat", new CatOwnerId(cat, catDto.getOwnerId()));
return new CatDto(catRepository.save(cat));
}

@Override
@RabbitListener(queues = "deleteByIdQueue")
public void deleteById(OwnerIdCatId ids) {
if (ids.getOwnerId() != catRepository.getById(ids.getCatId()).getOwner().getId()){
throw new ValidationException("Cat " + ids.getCatId() + " not found");
}
catRepository.deleteById(ids.getCatId());
}

@Override
@RabbitListener(queues = "findByIdQueue")
public CatDto findById(OwnerIdCatId ids) {
Cat cat = catRepository.getById(ids.getCatId());
if (ids.getOwnerId() != cat.getOwner().getId()){
throw new ValidationException("Cat " + ids.getCatId() + " not found");
}
return new CatDto(cat);
}

@Override
@RabbitListener(queues = "findAllByOwnerQueue")
public List<CatDto> findAllByOwner(int ownerId) {
List<CatDto> catsDto = new ArrayList<>();
for(Cat cat : catRepository.findAllByOwnerId(ownerId)) {
catsDto.add(new CatDto(cat));
}
return catsDto;
}

private boolean checkColor(String color) {
for (Color c : Color.values()) {
if (c.name().equals(color)) {
return true;
}
}
return false;
}

private void validateCatDto(CatDto catDto) throws ValidationException {
if (isNull(catDto)) {
throw new ValidationException("Object cat is null");
}
if (!checkColor(catDto.getColor())) {
throw new ValidationException("Color not found");
}
}

}
8 changes: 8 additions & 0 deletions kotiki-java/cats/core/src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
server:
port: 8081
spring:
rabbitmq:
host: localhost
password: guest
port: 15672
username: guest
24 changes: 24 additions & 0 deletions kotiki-java/cats/data/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>cats</artifactId>
<groupId>ru.itmo.kotiki.cats</groupId>
<version>1.0-SNAPSHOT</version>
</parent>

<modelVersion>4.0.0</modelVersion>
<groupId>ru.itmo.kotiki.cats.data</groupId>
<artifactId>cats-data</artifactId>
<packaging>pom</packaging>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>2.6.7</version>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package ru.itmo.kotiki.repository;
package ru.itmo.kotiki.cats.repository;

import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import ru.itmo.kotiki.model.Cat;

@Repository
public interface CatRepository extends JpaRepository<Cat, Integer> {

List<Cat> findAllByOwnerId(int ownerId);
}
}
Binary file not shown.
Loading