diff --git a/boot-api-archunit-sample/src/main/java/com/example/archunit/ExampleArchunitApplication.java b/boot-api-archunit-sample/src/main/java/com/example/archunit/ExampleArchUnitApplication.java similarity index 53% rename from boot-api-archunit-sample/src/main/java/com/example/archunit/ExampleArchunitApplication.java rename to boot-api-archunit-sample/src/main/java/com/example/archunit/ExampleArchUnitApplication.java index 0a8f2ccc5..189720bca 100644 --- a/boot-api-archunit-sample/src/main/java/com/example/archunit/ExampleArchunitApplication.java +++ b/boot-api-archunit-sample/src/main/java/com/example/archunit/ExampleArchUnitApplication.java @@ -1,14 +1,12 @@ -package com.example.archunit; - -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.context.annotation.ComponentScan; - -@SpringBootApplication -@ComponentScan({"com.example.archunit"}) -public class ExampleArchunitApplication { - - public static void main(String[] args) { - SpringApplication.run(ExampleArchunitApplication.class, args); - } -} +package com.example.archunit; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class ExampleArchUnitApplication { + + public static void main(String[] args) { + SpringApplication.run(ExampleArchUnitApplication.class, args); + } +} diff --git a/boot-api-archunit-sample/src/main/java/com/example/archunit/controller/ClientController.java b/boot-api-archunit-sample/src/main/java/com/example/archunit/controller/ClientController.java index ab24780e7..3d8d63d25 100644 --- a/boot-api-archunit-sample/src/main/java/com/example/archunit/controller/ClientController.java +++ b/boot-api-archunit-sample/src/main/java/com/example/archunit/controller/ClientController.java @@ -1,28 +1,28 @@ -package com.example.archunit.controller; - -import com.example.archunit.service.ClientService; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.DeleteMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.ResponseBody; -import org.springframework.web.bind.annotation.RestController; - -@RestController -@RequestMapping(value = "/client") -public class ClientController { - - ClientService clientService; - - @Autowired - public ClientController(ClientService clientService) { - this.clientService = clientService; - } - - @DeleteMapping(value = "/{id}") - public @ResponseBody ResponseEntity delete(@PathVariable("id") Long id) { - return new ResponseEntity<>(clientService.delete(id), HttpStatus.OK); - } -} +package com.example.archunit.controller; + +import com.example.archunit.service.ClientService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping(value = "/client") +public class ClientController { + + private final ClientService clientService; + + @Autowired + public ClientController(ClientService clientService) { + this.clientService = clientService; + } + + @DeleteMapping(value = "/{id}") + public @ResponseBody ResponseEntity delete(@PathVariable("id") Long id) { + return new ResponseEntity<>(clientService.delete(id), HttpStatus.OK); + } +} diff --git a/boot-api-archunit-sample/src/main/java/com/example/archunit/model/Base.java b/boot-api-archunit-sample/src/main/java/com/example/archunit/model/Base.java index 63c48b2e5..9033f8e8a 100644 --- a/boot-api-archunit-sample/src/main/java/com/example/archunit/model/Base.java +++ b/boot-api-archunit-sample/src/main/java/com/example/archunit/model/Base.java @@ -1,61 +1,62 @@ -package com.example.archunit.model; - -import jakarta.persistence.Column; -import jakarta.persistence.GeneratedValue; -import jakarta.persistence.GenerationType; -import jakarta.persistence.Id; -import jakarta.persistence.MappedSuperclass; -import jakarta.persistence.Version; -import java.io.Serializable; -import java.util.Objects; - -@MappedSuperclass -public abstract class Base implements Serializable { - - private static final long serialVersionUID = -2053886894431223968L; - - @Id - @GeneratedValue(strategy = GenerationType.IDENTITY) - private Long id; - - @Version - @Column(nullable = false) - private Integer version; - - @Column(nullable = false, columnDefinition = "boolean default true") - private Boolean active = Boolean.TRUE; - - public Long getId() { - return id; - } - - public void setId(Long id) { - this.id = id; - } - - public Integer getVersion() { - return version; - } - - public void setVersion(Integer version) { - this.version = version; - } - - public Boolean getActive() { - return active; - } - - public void setActive(Boolean active) { - this.active = active; - } - - @Override - public int hashCode() { - return Objects.hash(id, active, version); - } - - @Override - public boolean equals(Object obj) { - return Objects.equals(this, obj); - } -} +package com.example.archunit.model; + +import jakarta.persistence.Column; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import jakarta.persistence.MappedSuperclass; +import jakarta.persistence.Version; +import java.io.Serial; +import java.io.Serializable; +import java.util.Objects; + +@MappedSuperclass +public abstract class Base implements Serializable { + + @Serial protected static final long serialVersionUID = -2053886894431223968L; + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + protected Long id; + + @Version + @Column(nullable = false) + private Integer version; + + @Column(nullable = false, columnDefinition = "boolean default true") + private Boolean active = Boolean.TRUE; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public Integer getVersion() { + return version; + } + + public void setVersion(Integer version) { + this.version = version; + } + + public Boolean getActive() { + return active; + } + + public void setActive(Boolean active) { + this.active = active; + } + + @Override + public int hashCode() { + return Objects.hash(id, active, version); + } + + @Override + public boolean equals(Object obj) { + return Objects.equals(this, obj); + } +} diff --git a/boot-api-archunit-sample/src/main/java/com/example/archunit/model/Client.java b/boot-api-archunit-sample/src/main/java/com/example/archunit/model/Client.java index 860ebc8ad..35e8041a8 100644 --- a/boot-api-archunit-sample/src/main/java/com/example/archunit/model/Client.java +++ b/boot-api-archunit-sample/src/main/java/com/example/archunit/model/Client.java @@ -1,122 +1,120 @@ -package com.example.archunit.model; - -import jakarta.persistence.CascadeType; -import jakarta.persistence.Column; -import jakarta.persistence.Entity; -import jakarta.persistence.FetchType; -import jakarta.persistence.OneToMany; -import jakarta.persistence.Table; -import java.util.List; -import java.util.Objects; - -@Entity -@Table(name = "client") -public class Client extends Base { - - private static final long serialVersionUID = -2974615880078954663L; - - @Column(nullable = false, length = 100) - private String firstname; - - @Column(nullable = false, length = 100) - private String lastname; - - @Column(nullable = true, length = 100) - private String documentType; - - @Column(nullable = true, length = 100) - private String documentNumber; - - @OneToMany( - fetch = FetchType.LAZY, - mappedBy = "client", - orphanRemoval = true, - cascade = CascadeType.ALL) - private List phones; - - @OneToMany( - fetch = FetchType.LAZY, - mappedBy = "client", - orphanRemoval = true, - cascade = CascadeType.ALL) - private List emails; - - @Column(nullable = true, length = 100) - private String observation; - - public String getFirstname() { - return firstname; - } - - public void setFirstname(String firstname) { - this.firstname = firstname; - } - - public String getLastname() { - return lastname; - } - - public void setLastname(String lastname) { - this.lastname = lastname; - } - - public List getPhones() { - return phones; - } - - public void setPhones(List phones) { - this.phones = phones; - } - - public String getObservation() { - return observation; - } - - public void setObservation(String observation) { - this.observation = observation; - } - - public String getDocumentType() { - return documentType; - } - - public void setDocumentType(String documentType) { - this.documentType = documentType; - } - - public String getDocumentNumber() { - return documentNumber; - } - - public void setDocumentNumber(String documentNumber) { - this.documentNumber = documentNumber; - } - - public List getEmails() { - return emails; - } - - public void setEmails(List emails) { - this.emails = emails; - } - - @Override - public int hashCode() { - return Objects.hash( - this.getId(), - this.getActive(), - this.getVersion(), - firstname, - lastname, - documentType, - documentNumber, - phones, - emails, - observation); - } - - @Override - public boolean equals(Object obj) { - return Objects.equals(this, obj); - } -} +package com.example.archunit.model; + +import jakarta.persistence.CascadeType; +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.FetchType; +import jakarta.persistence.OneToMany; +import jakarta.persistence.Table; +import java.util.List; +import java.util.Objects; + +@Entity +@Table(name = "client") +public class Client extends Base { + + @Column(nullable = false, length = 100) + private String firstname; + + @Column(nullable = false, length = 100) + private String lastname; + + @Column(nullable = true, length = 100) + private String documentType; + + @Column(nullable = true, length = 100) + private String documentNumber; + + @OneToMany( + fetch = FetchType.LAZY, + mappedBy = "client", + orphanRemoval = true, + cascade = CascadeType.ALL) + private List phones; + + @OneToMany( + fetch = FetchType.LAZY, + mappedBy = "client", + orphanRemoval = true, + cascade = CascadeType.ALL) + private List emails; + + @Column(nullable = true, length = 100) + private String observation; + + public String getFirstname() { + return firstname; + } + + public void setFirstname(String firstname) { + this.firstname = firstname; + } + + public String getLastname() { + return lastname; + } + + public void setLastname(String lastname) { + this.lastname = lastname; + } + + public List getPhones() { + return phones; + } + + public void setPhones(List phones) { + this.phones = phones; + } + + public String getObservation() { + return observation; + } + + public void setObservation(String observation) { + this.observation = observation; + } + + public String getDocumentType() { + return documentType; + } + + public void setDocumentType(String documentType) { + this.documentType = documentType; + } + + public String getDocumentNumber() { + return documentNumber; + } + + public void setDocumentNumber(String documentNumber) { + this.documentNumber = documentNumber; + } + + public List getEmails() { + return emails; + } + + public void setEmails(List emails) { + this.emails = emails; + } + + @Override + public int hashCode() { + return Objects.hash( + this.getId(), + this.getActive(), + this.getVersion(), + firstname, + lastname, + documentType, + documentNumber, + phones, + emails, + observation); + } + + @Override + public boolean equals(Object obj) { + return Objects.equals(this, obj); + } +} diff --git a/boot-api-archunit-sample/src/main/java/com/example/archunit/model/ClientEmail.java b/boot-api-archunit-sample/src/main/java/com/example/archunit/model/ClientEmail.java index fc2b2afab..af6076b67 100644 --- a/boot-api-archunit-sample/src/main/java/com/example/archunit/model/ClientEmail.java +++ b/boot-api-archunit-sample/src/main/java/com/example/archunit/model/ClientEmail.java @@ -1,59 +1,57 @@ -package com.example.archunit.model; - -import jakarta.persistence.Column; -import jakarta.persistence.Entity; -import jakarta.persistence.FetchType; -import jakarta.persistence.ManyToOne; -import jakarta.persistence.Table; -import java.util.Objects; - -@Entity -@Table(name = "clientEmail") -public class ClientEmail extends Base { - - private static final long serialVersionUID = 1396542167093193958L; - - @Column(nullable = false, length = 80) - private String email; - - @ManyToOne(fetch = FetchType.LAZY) - private EmailType emailType; - - @ManyToOne(fetch = FetchType.LAZY) - private Client client; - - public String getEmail() { - return email; - } - - public void setEmail(String email) { - this.email = email; - } - - public EmailType getEmailType() { - return emailType; - } - - public void setEmailType(EmailType emailType) { - this.emailType = emailType; - } - - public Client getClient() { - return client; - } - - public void setClient(Client client) { - this.client = client; - } - - @Override - public int hashCode() { - return Objects.hash( - this.getId(), this.getActive(), this.getVersion(), email, emailType, client); - } - - @Override - public boolean equals(Object obj) { - return Objects.equals(this, obj); - } -} +package com.example.archunit.model; + +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.FetchType; +import jakarta.persistence.ManyToOne; +import jakarta.persistence.Table; +import java.util.Objects; + +@Entity +@Table(name = "clientEmail") +public class ClientEmail extends Base { + + @Column(nullable = false, length = 80) + private String email; + + @ManyToOne(fetch = FetchType.LAZY) + private EmailType emailType; + + @ManyToOne(fetch = FetchType.LAZY) + private Client client; + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + + public EmailType getEmailType() { + return emailType; + } + + public void setEmailType(EmailType emailType) { + this.emailType = emailType; + } + + public Client getClient() { + return client; + } + + public void setClient(Client client) { + this.client = client; + } + + @Override + public int hashCode() { + return Objects.hash( + this.getId(), this.getActive(), this.getVersion(), email, emailType, client); + } + + @Override + public boolean equals(Object obj) { + return Objects.equals(this, obj); + } +} diff --git a/boot-api-archunit-sample/src/main/java/com/example/archunit/model/ClientPhone.java b/boot-api-archunit-sample/src/main/java/com/example/archunit/model/ClientPhone.java index fa6b421d5..1f68ca939 100644 --- a/boot-api-archunit-sample/src/main/java/com/example/archunit/model/ClientPhone.java +++ b/boot-api-archunit-sample/src/main/java/com/example/archunit/model/ClientPhone.java @@ -1,59 +1,57 @@ -package com.example.archunit.model; - -import jakarta.persistence.Column; -import jakarta.persistence.Entity; -import jakarta.persistence.FetchType; -import jakarta.persistence.ManyToOne; -import jakarta.persistence.Table; -import java.util.Objects; - -@Entity -@Table(name = "clientPhone") -public class ClientPhone extends Base { - - private static final long serialVersionUID = 1396542167093193958L; - - @Column(nullable = false, length = 80) - private String number; - - @ManyToOne(fetch = FetchType.LAZY) - private PhoneType phoneType; - - @ManyToOne(fetch = FetchType.LAZY) - private Client client; - - public String getNumber() { - return number; - } - - public void setNumber(String number) { - this.number = number; - } - - public PhoneType getPhoneType() { - return phoneType; - } - - public void setPhoneType(PhoneType phoneType) { - this.phoneType = phoneType; - } - - public Client getClient() { - return client; - } - - public void setClient(Client client) { - this.client = client; - } - - @Override - public int hashCode() { - return Objects.hash( - this.getId(), this.getActive(), this.getVersion(), number, phoneType, client); - } - - @Override - public boolean equals(Object obj) { - return Objects.equals(this, obj); - } -} +package com.example.archunit.model; + +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.FetchType; +import jakarta.persistence.ManyToOne; +import jakarta.persistence.Table; +import java.util.Objects; + +@Entity +@Table(name = "clientPhone") +public class ClientPhone extends Base { + + @Column(nullable = false, length = 80) + private String number; + + @ManyToOne(fetch = FetchType.LAZY) + private PhoneType phoneType; + + @ManyToOne(fetch = FetchType.LAZY) + private Client client; + + public String getNumber() { + return number; + } + + public void setNumber(String number) { + this.number = number; + } + + public PhoneType getPhoneType() { + return phoneType; + } + + public void setPhoneType(PhoneType phoneType) { + this.phoneType = phoneType; + } + + public Client getClient() { + return client; + } + + public void setClient(Client client) { + this.client = client; + } + + @Override + public int hashCode() { + return Objects.hash( + this.getId(), this.getActive(), this.getVersion(), number, phoneType, client); + } + + @Override + public boolean equals(Object obj) { + return Objects.equals(this, obj); + } +} diff --git a/boot-api-archunit-sample/src/main/java/com/example/archunit/model/EmailType.java b/boot-api-archunit-sample/src/main/java/com/example/archunit/model/EmailType.java index b5d717034..3ad96ff07 100644 --- a/boot-api-archunit-sample/src/main/java/com/example/archunit/model/EmailType.java +++ b/boot-api-archunit-sample/src/main/java/com/example/archunit/model/EmailType.java @@ -1,34 +1,32 @@ -package com.example.archunit.model; - -import jakarta.persistence.Column; -import jakarta.persistence.Entity; -import jakarta.persistence.Table; -import java.util.Objects; - -@Entity -@Table(name = "emailType") -public class EmailType extends Base { - - private static final long serialVersionUID = 1697687804373017457L; - - @Column(nullable = false, length = 250) - private String name; - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - @Override - public int hashCode() { - return Objects.hash(this.getId(), this.getActive(), this.getVersion(), name); - } - - @Override - public boolean equals(Object obj) { - return Objects.equals(this, obj); - } -} +package com.example.archunit.model; + +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.Table; +import java.util.Objects; + +@Entity +@Table(name = "emailType") +public class EmailType extends Base { + + @Column(nullable = false, length = 250) + private String name; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + @Override + public int hashCode() { + return Objects.hash(this.getId(), this.getActive(), this.getVersion(), name); + } + + @Override + public boolean equals(Object obj) { + return Objects.equals(this, obj); + } +} diff --git a/boot-api-archunit-sample/src/main/java/com/example/archunit/model/PhoneType.java b/boot-api-archunit-sample/src/main/java/com/example/archunit/model/PhoneType.java index 347667b4c..d3f54b4a9 100644 --- a/boot-api-archunit-sample/src/main/java/com/example/archunit/model/PhoneType.java +++ b/boot-api-archunit-sample/src/main/java/com/example/archunit/model/PhoneType.java @@ -1,47 +1,30 @@ -package com.example.archunit.model; - -import jakarta.persistence.*; -import java.util.Objects; - -@Entity -@Table(name = "phoneType") -public class PhoneType extends Base { - - private static final long serialVersionUID = 1697687804373017457L; - - @Id - @GeneratedValue(strategy = GenerationType.AUTO) - @Column(name = "id", nullable = false) - private Long id; - - @Column(nullable = false, length = 250) - private String name; - - @Override - public Long getId() { - return id; - } - - @Override - public void setId(Long id) { - this.id = id; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - @Override - public int hashCode() { - return Objects.hash(this.getId(), this.getActive(), this.getVersion(), name); - } - - @Override - public boolean equals(Object obj) { - return Objects.equals(this, obj); - } -} +package com.example.archunit.model; + +import jakarta.persistence.*; +import java.util.Objects; + +@Entity +@Table(name = "phoneType") +public class PhoneType extends Base { + + @Column(nullable = false, length = 250) + private String name; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + @Override + public int hashCode() { + return Objects.hash(this.getId(), this.getActive(), this.getVersion(), name); + } + + @Override + public boolean equals(Object obj) { + return Objects.equals(this, obj); + } +} diff --git a/boot-api-archunit-sample/src/main/java/com/example/archunit/service/ClientService.java b/boot-api-archunit-sample/src/main/java/com/example/archunit/service/ClientService.java index c6c33c618..82c587e4e 100644 --- a/boot-api-archunit-sample/src/main/java/com/example/archunit/service/ClientService.java +++ b/boot-api-archunit-sample/src/main/java/com/example/archunit/service/ClientService.java @@ -1,31 +1,31 @@ -package com.example.archunit.service; - -import com.example.archunit.model.Client; -import com.example.archunit.repository.ClientRepository; -import java.util.Optional; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; - -@Service -public class ClientService { - - ClientRepository clientRepository; - - @Autowired - public ClientService(ClientRepository clientRepository) { - this.clientRepository = clientRepository; - } - - public Boolean delete(Long id) { - - Optional client = clientRepository.findById(id); - - if (client.isPresent()) { - client.get().setActive(Boolean.FALSE); - clientRepository.save(client.get()); - return Boolean.TRUE; - } - - return Boolean.FALSE; - } -} +package com.example.archunit.service; + +import com.example.archunit.model.Client; +import com.example.archunit.repository.ClientRepository; +import java.util.Optional; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +@Service +public class ClientService { + + private final ClientRepository clientRepository; + + @Autowired + public ClientService(ClientRepository clientRepository) { + this.clientRepository = clientRepository; + } + + public Boolean delete(Long id) { + + Optional client = clientRepository.findById(id); + + if (client.isPresent()) { + client.get().setActive(Boolean.FALSE); + clientRepository.save(client.get()); + return Boolean.TRUE; + } + + return Boolean.FALSE; + } +}