Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scrum 83 #102

Merged
merged 19 commits into from
Sep 7, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
import com.greenfoxacademy.backend.errors.CannotUpdateUserException;
import com.greenfoxacademy.backend.errors.UnableToDeleteProfileError;
import com.greenfoxacademy.backend.errors.UserAlreadyExistsError;

import java.util.HashMap;
import java.util.Map;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.FieldError;
Expand Down Expand Up @@ -75,6 +73,7 @@ public ResponseEntity<Map<String, String>> handleCannotUpdateUserExceptions(
*
* @return ResponseEntity with BAD_REQUEST and error key-value pair
*/

@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(UnableToDeleteProfileError.class)
public ResponseEntity<?> handleUnableToDeleteProfileError(UnableToDeleteProfileError ex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@
import com.greenfoxacademy.backend.errors.CannotUpdateUserException;
import com.greenfoxacademy.backend.errors.UnableToDeleteProfileError;
import com.greenfoxacademy.backend.errors.UserAlreadyExistsError;
import com.greenfoxacademy.backend.services.user.UserService;

import com.greenfoxacademy.backend.services.user.owner.OwnerService;
import java.security.Principal;
import java.util.UUID;

import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
Expand All @@ -32,7 +30,7 @@
@RestController
@RequiredArgsConstructor
public class UserController {
private final UserService userService;
private final OwnerService ownerService;

/**
* This method registers a new user.
Expand All @@ -44,7 +42,7 @@ public class UserController {
public ResponseEntity<RegisterResponseDto> registerUser(
@Validated @RequestBody RegisterRequestDto registerRequestDto
)throws UserAlreadyExistsError {
return ResponseEntity.status(HttpStatus.OK).body(userService.register(registerRequestDto));
return ResponseEntity.status(HttpStatus.OK).body(ownerService.register(registerRequestDto));
}

/**
Expand All @@ -59,7 +57,7 @@ public ResponseEntity<RegisterResponseDto> registerUser(
@PostMapping("/login")
public ResponseEntity<LoginResponseDto> loginUser(@RequestBody LoginRequestDto loginRequestDto) {
try {
return ResponseEntity.status(HttpStatus.OK).body(userService.login(loginRequestDto));
return ResponseEntity.status(HttpStatus.OK).body(ownerService.login(loginRequestDto));
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
}
Expand All @@ -80,7 +78,7 @@ public ResponseEntity<ProfileUpdateResponseDto> userProfileUpdate(
) throws CannotUpdateUserException {
return ResponseEntity
.status(HttpStatus.OK)
.body(userService.profileUpdate(principal.getName(), profileUpdateRequestDto));
.body(ownerService.profileUpdate(principal.getName(), profileUpdateRequestDto));
}

/**
Expand All @@ -91,14 +89,14 @@ public ResponseEntity<ProfileUpdateResponseDto> userProfileUpdate(
*/
@DeleteMapping("/delete-profile")
public ResponseEntity<?> deleteProfile(Principal principal) throws UnableToDeleteProfileError {
userService.deleteProfile(principal.getName());
ownerService.deleteProfile(principal.getName());
return ResponseEntity.status(HttpStatus.ACCEPTED).build();
}

// http://localhost:8080/verification?code=56565-55656-56-56-5-65-6-56
@GetMapping("/verification")
public ResponseEntity<?> verificationPage(@RequestParam(value = "code") UUID verificationCode) {
userService.verifyUser(verificationCode);
ownerService.verifyUser(verificationCode);
return ResponseEntity.status(HttpStatus.OK).build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.greenfoxacademy.backend.models;

import jakarta.persistence.Column;
import jakarta.persistence.Embeddable;
import jakarta.persistence.Id;
import lombok.Data;

/**
* Represents an address for a clinic.
* <p>
* The {@code ClinicAddress} class is used as an embeddable type in JPA entities to store address
* details of a clinic. It includes information such as the street address, city, postal code,
* and clinic name.
* </p>
* <p>
* This class is marked with {@code @Embeddable} to indicate that it can be embedded in other JPA
* entities.
* </p>
* <p>
* The {@code id} field is used as a unique identifier for the address. This field is optional and
* is provided for cases where a unique identifier is necessary.
* </p>
* <p>
* The {@code zip} field represents the postal code for the clinic address and is limited to a
* length of 4 digits.
* </p>
* <p>
* The {@code clinicName} field specifies the name of the clinic associated with the address.
* </p>
*
* @see jakarta.persistence.Embeddable
*/

@Embeddable
@Data
public class ClinicAddress {
@Id
private Long id;
private String city;
@Column(length = 4)
private int zip;
private String street;
private String clinicName;


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.greenfoxacademy.backend.models;

import jakarta.persistence.Embeddable;
import lombok.Data;

/**
* Represents the details of a clinic.
* <p>
* The {@code ClinicDetails} class is used as an embeddable type in JPA entities to store detailed
* information about a clinic. This class contains the address of the clinic as well as any
* additional details that may be relevant for clinic management.
* </p>
* <p>
* This class is annotated with {@code @Embeddable} to indicate that it can be embedded within
* other JPA entities.
* </p>
*
* @see ClinicAddress
*/

@Data
@Embeddable
public class ClinicDetails {

private ClinicAddress clinicAddress;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.greenfoxacademy.backend.models;

import jakarta.persistence.Entity;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
import jakarta.persistence.Transient;
import java.util.Collection;
import java.util.List;
import lombok.AllArgsConstructor;
import lombok.RequiredArgsConstructor;
import lombok.experimental.SuperBuilder;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;

/**
* Represents an owner in the system who is also a user.
* <p>
* The {@code Owner} class extends the {@code User} class and includes specific information related
* to an owner, such as the list of pets they own. It is marked with {@code @Entity} to indicate
* that it is a JPA entity and is mapped to a database table.
* </p>
* <p>
* The {@code @Table(name = "_owner")} annotation specifies the name of the database table
* associated with this entity. The table name is prefixed with an underscore to avoid conflicts
* with reserved SQL keywords.
* </p>
* <p>
* The {@code getAuthorities} method overrides the method from {@code User} to provide specific
* authorities for an owner, granting the role {@code ROLE_OWNER}.
* </p>
*
* @see User
*/

@Entity
@SuperBuilder
@RequiredArgsConstructor
@AllArgsConstructor
@Table(name = "_owner")
public class Owner extends User {
@OneToMany(mappedBy = "petOwner")
private List<Pet> pets;

@Override
@Transient
public Collection<? extends GrantedAuthority> getAuthorities() {
return List.of(new SimpleGrantedAuthority("ROLE_OWNER"));
}
}
44 changes: 44 additions & 0 deletions backend/src/main/java/com/greenfoxacademy/backend/models/Pet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.greenfoxacademy.backend.models;

import jakarta.persistence.CascadeType;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import java.util.Date;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
* Represents a pet entity in the system. This class is a data model
* that maps to the pet table in the database.
*/
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "_pet")
public class Pet {

@Id
@GeneratedValue
private Integer id;

@Column(nullable = false)
private String petName;
private String petBreed;
private String petSex;
private Date petBirthDate;
private Date lastCheckUp;
private Date nextCheckUp;

@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "petOwner_Id")
private Owner petOwner;
}
21 changes: 5 additions & 16 deletions backend/src/main/java/com/greenfoxacademy/backend/models/User.java
Original file line number Diff line number Diff line change
@@ -1,32 +1,26 @@
package com.greenfoxacademy.backend.models;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import java.util.Collection;
import java.util.List;
import jakarta.persistence.MappedSuperclass;
import java.util.UUID;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import lombok.experimental.SuperBuilder;
import org.springframework.security.core.userdetails.UserDetails;

/**
* Represents a user entity in the system. This class is a data model
* that maps to the user table in the database.
*/
@Data
@Builder
@SuperBuilder
@MappedSuperclass
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "_user")
public class User implements UserDetails {
public abstract class User implements UserDetails {

@Id
@GeneratedValue
Expand All @@ -38,11 +32,6 @@ public class User implements UserDetails {
private String password;
private UUID verificationId;

@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
return List.of(new SimpleGrantedAuthority("ROLE_USER"));
}

@Override
public String getUsername() {
return email;
Expand Down
47 changes: 47 additions & 0 deletions backend/src/main/java/com/greenfoxacademy/backend/models/Vet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.greenfoxacademy.backend.models;

import jakarta.persistence.Entity;
import jakarta.persistence.Table;
import jakarta.persistence.Transient;
import java.util.Collection;
import java.util.List;
import lombok.AllArgsConstructor;
import lombok.RequiredArgsConstructor;
import lombok.experimental.SuperBuilder;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;

/**
* Represents a veterinarian who is also a user in the system.
* <p>
* The {@code Vet} class extends the {@code User} class and adds specific information related to
* a veterinarian, such as the clinic address. It is annotated with {@code @Entity} to indicate
* that it is a JPA entity and will be mapped to a database table.
* </p>
* <p>
* This class is annotated with {@code @Table(name = "_vet")} to specify the name of the database
* table to which this entity is mapped. The table name is prefixed with an underscore to avoid
* conflicts with reserved SQL keywords.
* </p>
* <p>
* The {@code getAuthorities} method overrides the method from {@code User} to provide specific
* authorities for a veterinarian, in this case, granting the role {@code ROLE_VET}.
* </p>
*
* @see User
*/

@SuperBuilder
@RequiredArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "_vet")
public class Vet extends User {
private ClinicDetails clinicDetails;

@Override
@Transient
public Collection<? extends GrantedAuthority> getAuthorities() {
return List.of(new SimpleGrantedAuthority("ROLE_VET"));
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
package com.greenfoxacademy.backend.repositories;

import com.greenfoxacademy.backend.models.Owner;
import com.greenfoxacademy.backend.models.User;
import java.util.Optional;
import java.util.UUID;

import org.springframework.data.jpa.repository.JpaRepository;

/**
* Repository to manage {@link User} entities.
*/
public interface UserRepository extends JpaRepository<User, Integer> {
public interface OwnerRepository extends JpaRepository<Owner, Integer> {
boolean existsByEmail(String email);

Optional<User> findByEmail(String email);
Optional<Owner> findByEmail(String email);

void deleteByEmail(String email);

Optional<User> findByVerificationId(UUID id);
Optional<Owner> findByVerificationId(UUID id);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.greenfoxacademy.backend.repositories;

import com.greenfoxacademy.backend.models.Pet;
import org.springframework.data.jpa.repository.JpaRepository;

/**
* Repository to manage Pet entities.
*/
public interface PetRepository extends JpaRepository<Pet, Integer> {
}
Loading
Loading