Skip to content

Commit

Permalink
Refactoring & clean up (#201)
Browse files Browse the repository at this point in the history
* fix: telephone input field pattern & size conditions set & updated on db script

* chore: clean up

* feat: min validations added for ids

* fix: typo

* fix: indentation up
  • Loading branch information
ramazansakin authored Feb 27, 2022
1 parent 05007a6 commit 8685f93
Show file tree
Hide file tree
Showing 13 changed files with 82 additions and 185 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)

This microservices branch was initially derived from [AngularJS version](https://github.com/spring-petclinic/spring-petclinic-angular1) to demonstrate how to split sample Spring application into [microservices](http://www.martinfowler.com/articles/microservices.html).
To achieve that goal we use Spring Cloud Gateway, Spring Cloud Circuit Breaker, Spring Cloud Config, Spring Cloud Sleuth, Resilience4j, Micrometer
To achieve that goal, we use Spring Cloud Gateway, Spring Cloud Circuit Breaker, Spring Cloud Config, Spring Cloud Sleuth, Resilience4j, Micrometer
and the Eureka Service Discovery from the [Spring Cloud Netflix](https://github.com/spring-cloud/spring-cloud-netflix) technology stack.

## Starting services locally without Docker
Expand All @@ -29,7 +29,7 @@ You can tell Config Server to use your local Git repository by using `native` Sp
In order to start entire infrastructure using Docker, you have to build images by executing `./mvnw clean install -P buildDocker`
from a project root. Once images are ready, you can start them with a single command
`docker-compose up`. Containers startup order is coordinated with [`dockerize` script](https://github.com/jwilder/dockerize).
After starting services it takes a while for API Gateway to be in sync with service registry,
After starting services, it takes a while for API Gateway to be in sync with service registry,
so don't be scared of initial Spring Cloud Gateway timeouts. You can track services availability using Eureka dashboard
available by default at http://localhost:8761.

Expand All @@ -48,7 +48,7 @@ Each of the java based applications is started with the `chaos-monkey` profile i

[See the presentation of the Spring Petclinic Framework version](http://fr.slideshare.net/AntoineRey/spring-framework-petclinic-sample-application)

[A blog bost introducing the Spring Petclinic Microsevices](http://javaetmoi.com/2018/10/architecture-microservices-avec-spring-cloud/) (french language)
[A blog post introducing the Spring Petclinic Microsevices](http://javaetmoi.com/2018/10/architecture-microservices-avec-spring-cloud/) (french language)

You can then access petclinic here: http://localhost:8080/

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import lombok.RequiredArgsConstructor;
import org.springframework.samples.petclinic.api.dto.OwnerDetails;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ <h2>Owner</h2>

<div class="form-group">
<label>Telephone</label>
<input class="form-control" ng-model="$ctrl.owner.telephone" name="telephone" required/>
<input class="form-control" ng-model="$ctrl.owner.telephone" pattern="[0-9]{12}" placeholder="905554443322"
name="telephone" maxlength="12" required/>
<span ng-show="ownerForm.telephone.$error.required" class="help-block">Telephone is required.</span>
</div>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.validation.constraints.Digits;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotBlank;

import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.support.MutableSortDefinition;
import org.springframework.beans.support.PropertyComparator;
import org.springframework.core.style.ToStringCreator;
Expand All @@ -45,83 +47,51 @@
* @author Sam Brannen
* @author Michael Isvy
* @author Maciej Szarlinski
* @author Ramazan Sakin
*/
@Entity
@Table(name = "owners")
public class Owner {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Getter
private Integer id;

@Getter
@Setter
@Column(name = "first_name")
@NotEmpty
@NotBlank
private String firstName;

@Getter
@Setter
@Column(name = "last_name")
@NotEmpty
@NotBlank
private String lastName;

@Getter
@Setter
@Column(name = "address")
@NotEmpty
@NotBlank
private String address;

@Getter
@Setter
@Column(name = "city")
@NotEmpty
@NotBlank
private String city;

@Getter
@Setter
@Column(name = "telephone")
@NotEmpty
@Digits(fraction = 0, integer = 10)
@NotBlank
@Digits(fraction = 0, integer = 12)
private String telephone;

@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "owner")
private Set<Pet> pets;

public Integer getId() {
return id;
}

public String getFirstName() {
return firstName;
}

public void setFirstName(final String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}

public void setLastName(final String lastName) {
this.lastName = lastName;
}

public String getAddress() {
return this.address;
}

public void setAddress(String address) {
this.address = address;
}

public String getCity() {
return this.city;
}

public void setCity(String city) {
this.city = city;
}

public String getTelephone() {
return this.telephone;
}

public void setTelephone(String telephone) {
this.telephone = telephone;
}

protected Set<Pet> getPetsInternal() {
if (this.pets == null) {
this.pets = new HashSet<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import javax.persistence.TemporalType;

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;
import org.springframework.core.style.ToStringCreator;

/**
Expand All @@ -38,7 +39,9 @@
* @author Juergen Hoeller
* @author Sam Brannen
* @author Maciej Szarlinski
* @author Ramazan Sakin
*/
@Data
@Entity
@Table(name = "pets")
public class Pet {
Expand All @@ -62,46 +65,6 @@ public class Pet {
@JsonIgnore
private Owner owner;

public Integer getId() {
return id;
}

public void setId(final Integer id) {
this.id = id;
}

public String getName() {
return this.name;
}

public void setName(final String name) {
this.name = name;
}

public Date getBirthDate() {
return birthDate;
}

public void setBirthDate(final Date birthDate) {
this.birthDate = birthDate;
}

public PetType getType() {
return type;
}

public void setType(final PetType type) {
this.type = type;
}

public Owner getOwner() {
return owner;
}

public void setOwner(final Owner owner) {
this.owner = owner;
}

@Override
public String toString() {
return new ToStringCreator(this)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
*/
package org.springframework.samples.petclinic.customers.model;

import lombok.Getter;
import lombok.Setter;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
Expand All @@ -24,27 +27,21 @@

/**
* @author Juergen Hoeller
* @author Ramazan Sakin
* Can be Cat, Dog, Hamster...
*/
@Entity
@Table(name = "types")
public class PetType {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Getter
@Setter
private Integer id;

@Getter
@Setter
@Column(name = "name")
private String name;

public Integer getId() {
return id;
}

public void setId(final Integer id) {
this.id = id;
}

public String getName() {
return this.name;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.Min;
import java.util.List;
import java.util.Optional;

Expand Down Expand Up @@ -56,7 +57,7 @@ public Owner createOwner(@Valid @RequestBody Owner owner) {
* Read single Owner
*/
@GetMapping(value = "/{ownerId}")
public Optional<Owner> findOwner(@PathVariable("ownerId") int ownerId) {
public Optional<Owner> findOwner(@PathVariable("ownerId") @Min(1) int ownerId) {
return ownerRepository.findById(ownerId);
}

Expand All @@ -73,10 +74,10 @@ public List<Owner> findAll() {
*/
@PutMapping(value = "/{ownerId}")
@ResponseStatus(HttpStatus.NO_CONTENT)
public void updateOwner(@PathVariable("ownerId") int ownerId, @Valid @RequestBody Owner ownerRequest) {
public void updateOwner(@PathVariable("ownerId") @Min(1) int ownerId, @Valid @RequestBody Owner ownerRequest) {
final Optional<Owner> owner = ownerRepository.findById(ownerId);

final Owner ownerModel = owner.orElseThrow(() -> new ResourceNotFoundException("Owner "+ownerId+" not found"));

// This is done by hand for simplicity purpose. In a real life use-case we should consider using MapStruct.
ownerModel.setFirstName(ownerRequest.getFirstName());
ownerModel.setLastName(ownerRequest.getLastName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.springframework.samples.petclinic.customers.model.*;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Min;
import java.util.List;
import java.util.Optional;

Expand All @@ -30,6 +31,7 @@
* @author Ken Krebs
* @author Arjen Poutsma
* @author Maciej Szarlinski
* @author Ramazan Sakin
*/
@RestController
@Timed("petclinic.pet")
Expand All @@ -50,13 +52,13 @@ public List<PetType> getPetTypes() {
@ResponseStatus(HttpStatus.CREATED)
public Pet processCreationForm(
@RequestBody PetRequest petRequest,
@PathVariable("ownerId") int ownerId) {
@PathVariable("ownerId") @Min(1) int ownerId) {

final Pet pet = new Pet();
final Optional<Owner> optionalOwner = ownerRepository.findById(ownerId);
Owner owner = optionalOwner.orElseThrow(() -> new ResourceNotFoundException("Owner "+ownerId+" not found"));
owner.addPet(pet);

final Pet pet = new Pet();
owner.addPet(pet);
return save(pet, petRequest);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ CREATE TABLE owners (
last_name VARCHAR(30),
address VARCHAR(255),
city VARCHAR(80),
telephone VARCHAR(20)
telephone VARCHAR(12)
);
CREATE INDEX owners_last_name ON owners (last_name);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
*/
package org.springframework.samples.petclinic.vets.model;

import lombok.Getter;
import lombok.Setter;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
Expand All @@ -26,30 +29,20 @@
* Models a {@link Vet Vet's} specialty (for example, dentistry).
*
* @author Juergen Hoeller
* @author Ramazan Sakin
*/

@Entity
@Table(name = "specialties")
public class Specialty {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Getter
private Integer id;

@Getter
@Setter
@Column(name = "name")
private String name;

public Integer getId() {
return id;
}

public void setId(final Integer id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(final String name) {
this.name = name;
}
}
Loading

0 comments on commit 8685f93

Please sign in to comment.