Skip to content

Commit

Permalink
clean unused endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
rajadilipkolli committed Nov 22, 2023
1 parent 7e8a3d0 commit 71773a6
Show file tree
Hide file tree
Showing 17 changed files with 30 additions and 544 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class Actor {
private Long id;

@Column(nullable = false)
private String text;
private String name;

@Override
public boolean equals(Object o) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

public class ActorNotFoundException extends ResourceNotFoundException {

public ActorNotFoundException(Long id) {
super("Actor with Id '%d' not found".formatted(id));
public ActorNotFoundException(String name) {
super("Actor with Name '%s' not found".formatted(name));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,18 @@
import com.learning.awslambda.entities.Actor;
import com.learning.awslambda.model.request.ActorRequest;
import com.learning.awslambda.model.response.ActorResponse;
import java.util.List;
import org.springframework.stereotype.Service;

@Service
public class ActorMapper {

public Actor toEntity(ActorRequest actorRequest) {
Actor actor = new Actor();
actor.setText(actorRequest.text());
actor.setName(actorRequest.name());
return actor;
}

public void mapActorWithRequest(Actor actor, ActorRequest actorRequest) {
actor.setText(actorRequest.text());
}

public ActorResponse toResponse(Actor actor) {
return new ActorResponse(actor.getId(), actor.getText());
}

public List<ActorResponse> toResponseList(List<Actor> actorList) {
return actorList.stream().map(this::toResponse).toList();
return new ActorResponse(actor.getId(), actor.getName());
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

import jakarta.validation.constraints.NotEmpty;

public record ActorRequest(@NotEmpty(message = "Text cannot be empty") String text) {}
public record ActorRequest(@NotEmpty(message = "Name cannot be empty") String name) {}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package com.learning.awslambda.model.response;

public record ActorResponse(Long id, String text) {}
public record ActorResponse(Long id, String name) {}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.learning.awslambda.repositories;

import com.learning.awslambda.entities.Actor;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;

public interface ActorRepository extends JpaRepository<Actor, Long> {}
public interface ActorRepository extends JpaRepository<Actor, Long> {
Optional<Actor> findByNameLike(String name);
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,10 @@
package com.learning.awslambda.services;

import com.learning.awslambda.entities.Actor;
import com.learning.awslambda.exception.ActorNotFoundException;
import com.learning.awslambda.mapper.ActorMapper;
import com.learning.awslambda.model.query.FindActorsQuery;
import com.learning.awslambda.model.request.ActorRequest;
import com.learning.awslambda.model.response.ActorResponse;
import com.learning.awslambda.model.response.PagedResult;
import com.learning.awslambda.repositories.ActorRepository;
import java.util.List;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -26,53 +16,7 @@ public class ActorService {
private final ActorRepository actorRepository;
private final ActorMapper actorMapper;

public PagedResult<ActorResponse> findAllActors(FindActorsQuery findActorsQuery) {

// create Pageable instance
Pageable pageable = createPageable(findActorsQuery);

Page<Actor> actorsPage = actorRepository.findAll(pageable);

List<ActorResponse> actorResponseList = actorMapper.toResponseList(actorsPage.getContent());

return new PagedResult<>(actorsPage, actorResponseList);
}

private Pageable createPageable(FindActorsQuery findActorsQuery) {
int pageNo = Math.max(findActorsQuery.pageNo() - 1, 0);
Sort sort = Sort.by(
findActorsQuery.sortDir().equalsIgnoreCase(Sort.Direction.ASC.name())
? Sort.Order.asc(findActorsQuery.sortBy())
: Sort.Order.desc(findActorsQuery.sortBy()));
return PageRequest.of(pageNo, findActorsQuery.pageSize(), sort);
}

public Optional<ActorResponse> findActorById(Long id) {
return actorRepository.findById(id).map(actorMapper::toResponse);
}

@Transactional
public ActorResponse saveActor(ActorRequest actorRequest) {
Actor actor = actorMapper.toEntity(actorRequest);
Actor savedActor = actorRepository.save(actor);
return actorMapper.toResponse(savedActor);
}

@Transactional
public ActorResponse updateActor(Long id, ActorRequest actorRequest) {
Actor actor = actorRepository.findById(id).orElseThrow(() -> new ActorNotFoundException(id));

// Update the actor object with data from actorRequest
actorMapper.mapActorWithRequest(actor, actorRequest);

// Save the updated actor object
Actor updatedActor = actorRepository.save(actor);

return actorMapper.toResponse(updatedActor);
}

@Transactional
public void deleteActorById(Long id) {
actorRepository.deleteById(id);
public Optional<ActorResponse> findActorByName(String name) {
return actorRepository.findByNameLike(name).map(actorMapper::toResponse);
}
}
Original file line number Diff line number Diff line change
@@ -1,80 +1,27 @@
package com.learning.awslambda.web.controllers;

import com.learning.awslambda.exception.ActorNotFoundException;
import com.learning.awslambda.model.query.FindActorsQuery;
import com.learning.awslambda.model.request.ActorRequest;
import com.learning.awslambda.model.response.ActorResponse;
import com.learning.awslambda.model.response.PagedResult;
import com.learning.awslambda.services.ActorService;
import com.learning.awslambda.utils.AppConstants;
import jakarta.validation.Valid;
import java.net.URI;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;

@RestController
@RequestMapping("/api/actors")
@Slf4j
@RequiredArgsConstructor
public class ActorController {

private final ActorService actorService;

@GetMapping
public PagedResult<ActorResponse> getAllActors(
@RequestParam(value = "pageNo", defaultValue = AppConstants.DEFAULT_PAGE_NUMBER, required = false)
int pageNo,
@RequestParam(value = "pageSize", defaultValue = AppConstants.DEFAULT_PAGE_SIZE, required = false)
int pageSize,
@RequestParam(value = "sortBy", defaultValue = AppConstants.DEFAULT_SORT_BY, required = false)
String sortBy,
@RequestParam(value = "sortDir", defaultValue = AppConstants.DEFAULT_SORT_DIRECTION, required = false)
String sortDir) {
FindActorsQuery findActorsQuery = new FindActorsQuery(pageNo, pageSize, sortBy, sortDir);
return actorService.findAllActors(findActorsQuery);
}

@GetMapping("/{id}")
public ResponseEntity<ActorResponse> getActorById(@PathVariable Long id) {
return actorService.findActorById(id).map(ResponseEntity::ok).orElseThrow(() -> new ActorNotFoundException(id));
}

@PostMapping
public ResponseEntity<ActorResponse> createActor(@RequestBody @Validated ActorRequest actorRequest) {
ActorResponse response = actorService.saveActor(actorRequest);
URI location = ServletUriComponentsBuilder.fromCurrentRequest()
.path("/api/actors/{id}")
.buildAndExpand(response.id())
.toUri();
return ResponseEntity.created(location).body(response);
}

@PutMapping("/{id}")
public ResponseEntity<ActorResponse> updateActor(
@PathVariable Long id, @RequestBody @Valid ActorRequest actorRequest) {
return ResponseEntity.ok(actorService.updateActor(id, actorRequest));
}

@DeleteMapping("/{id}")
public ResponseEntity<ActorResponse> deleteActor(@PathVariable Long id) {
@GetMapping("/{name}")
public ResponseEntity<ActorResponse> getActorByName(@PathVariable String name) {
return actorService
.findActorById(id)
.map(actor -> {
actorService.deleteActorById(id);
return ResponseEntity.ok(actor);
})
.orElseThrow(() -> new ActorNotFoundException(id));
.findActorByName(name)
.map(ResponseEntity::ok)
.orElseThrow(() -> new ActorNotFoundException(name));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<column name="id" type="bigint" defaultValueSequenceNext="actors_seq">
<constraints primaryKey="true" nullable="false"/>
</column>
<column name="text" type="${string.type}">
<column name="name" type="${string.type}">
<constraints nullable="false"/>
</column>
</createTable>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
properties = {
"spring.jpa.hibernate.ddl-auto=validate",
"spring.test.database.replace=none",
"spring.datasource.url=jdbc:tc:postgresql:16.0-alpine:///db"
"spring.datasource.url=jdbc:tc:postgresql:16.1-alpine:///db"
})
class SchemaValidationTest {

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ public class ContainersConfig {
@Bean
@ServiceConnection
PostgreSQLContainer<?> postgreSQLContainer() {
return new PostgreSQLContainer<>(DockerImageName.parse("postgres:16.0-alpine"));
return new PostgreSQLContainer<>(DockerImageName.parse("postgres:16.1-alpine"));
}

@Bean
LocalStackContainer localstackContainer(DynamicPropertyRegistry registry) {
LocalStackContainer localStackContainer =
new LocalStackContainer(DockerImageName.parse("localstack/localstack:2.3.2"));
new LocalStackContainer(DockerImageName.parse("localstack/localstack:3.0.0"));
registry.add("spring.cloud.aws.credentials.access-key", localStackContainer::getAccessKey);
registry.add("spring.cloud.aws.credentials.secret-key", localStackContainer::getSecretKey);
registry.add("spring.cloud.aws.region.static", localStackContainer::getRegion);
Expand Down
Loading

0 comments on commit 71773a6

Please sign in to comment.