Skip to content

Commit

Permalink
Saved necessary data from an external DB and implemented two methods …
Browse files Browse the repository at this point in the history
…in controller.
  • Loading branch information
vaniii1 committed Oct 18, 2023
1 parent c3bbe60 commit aa34f7d
Show file tree
Hide file tree
Showing 10 changed files with 123 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/main/java/mate/academy/rickandmorty/config/MapperConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package mate.academy.rickandmorty.controller.config;

import org.mapstruct.InjectionStrategy;
import org.mapstruct.NullValueCheckStrategy;

@org.mapstruct.MapperConfig(
componentModel = "spring",
injectionStrategy = InjectionStrategy.CONSTRUCTOR,
nullValueCheckStrategy = NullValueCheckStrategy.ALWAYS,
implementationPackage = "<PACKAGE_NAME>.impl"
)
public class MapperConfig {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
package mate.academy.rickandmorty.controller;public class RickAndMortyController {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package mate.academy.rickandmorty.dto.external;

public class CharacterResponseDto {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package mate.academy.rickandmorty.dto.external;

import lombok.Data;

@Data
public class RickAndMortyCharacter {
private Long id;
private String name;
private String status;
private String gender;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package mate.academy.rickandmorty.model;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import lombok.Data;

@Data
@Entity
@Table(name = "rick_and_morty_characters")
public class RickAndMortyCharacter {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private Long externalId;
private String name;
private String status;
private String gender;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package mate.academy.rickandmorty.controller.mapper;

import mate.academy.rickandmorty.controller.config.MapperConfig;
import mate.academy.rickandmorty.dto.external.RickAndMortyCharacterDto;
import mate.academy.rickandmorty.model.RickAndMortyCharacter;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;

@Mapper(config = MapperConfig.class)
public interface CharacterMapper {
@Mapping(target = "id", ignore = true)
@Mapping(target = "externalId", source = "id")
RickAndMortyCharacter toModel(RickAndMortyCharacterDto characterDto);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package mate.academy.rickandmorty.repository;

import mate.academy.rickandmorty.model.RickAndMortyCharacter;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;

public interface RickAndMortyCharacterRepository
extends JpaRepository<RickAndMortyCharacter, Long>,
JpaSpecificationExecutor<RickAndMortyCharacter> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
package mate.academy.rickandmorty.service;public class RickAndMortyClient {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package mate.academy.rickandmorty.service.rickandmortcharacter;

import mate.academy.rickandmorty.model.RickAndMortyCharacter;

public interface RickAndMortyCharacterService {
RickAndMortyCharacter getRandomCharacter();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package mate.academy.rickandmorty.service.rickandmortcharacter;

import jakarta.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
import mate.academy.rickandmorty.controller.mapper.CharacterMapper;
import mate.academy.rickandmorty.dto.external.RickAndMortyCharacterDto;
import mate.academy.rickandmorty.model.RickAndMortyCharacter;
import mate.academy.rickandmorty.repository.RickAndMortyCharacterRepository;
import mate.academy.rickandmorty.service.RickAndMortyClient;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Random;

@RequiredArgsConstructor
@Service
public class RickAndMortyCharacterServiceImpl implements RickAndMortyCharacterService {
private final RickAndMortyClient client;
private final RickAndMortyCharacterRepository repository;
private final CharacterMapper characterMapper;

@PostConstruct
public void saveAllCharacters() {
List<RickAndMortyCharacter> list =
client.getAllCharacter()
.stream()
.map(characterMapper::toModel)
.toList();
repository.saveAll(list);
}

@Override
public RickAndMortyCharacter getRandomCharacter() {
List<RickAndMortyCharacter> all = repository.findAll();
return all.get(new Random().nextInt(all.size()));
}
}

0 comments on commit aa34f7d

Please sign in to comment.