-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' of https://github.com/URECA-PODONG/BackEnd into
feat/#6
- Loading branch information
Showing
38 changed files
with
3,109 additions
and
20 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -69,3 +69,4 @@ public ResponseEntity<Void> deleteCommunityComment( | |
|
||
|
||
} | ||
|
19 changes: 19 additions & 0 deletions
19
src/main/java/com/ureca/sole_paradise/config/WebConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.ureca.sole_paradise.config; | ||
|
||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.web.servlet.config.annotation.CorsRegistry; | ||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; | ||
|
||
@Configuration | ||
public class WebConfig implements WebMvcConfigurer { | ||
|
||
@Override | ||
public void addCorsMappings(CorsRegistry registry) { | ||
registry.addMapping("/**") // 모든 하위 경로 포함 | ||
.allowedOriginPatterns("http://localhost:5173") | ||
.allowedMethods("GET", "POST", "PUT", "DELETE") | ||
.allowedHeaders("*") // 모든 헤더 허용 | ||
.allowCredentials(true); | ||
} | ||
|
||
} |
69 changes: 69 additions & 0 deletions
69
src/main/java/com/ureca/sole_paradise/health/controller/HealthController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package com.ureca.sole_paradise.health.controller; | ||
|
||
import java.util.List; | ||
|
||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.ResponseEntity; | ||
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.RestController; | ||
|
||
import com.ureca.sole_paradise.health.db.dto.HealthDTO; | ||
import com.ureca.sole_paradise.health.service.HealthService; | ||
|
||
import io.swagger.v3.oas.annotations.responses.ApiResponse; | ||
import jakarta.validation.Valid; | ||
|
||
|
||
@RestController | ||
@RequestMapping(value = "/api/healths", produces = MediaType.APPLICATION_JSON_VALUE) | ||
|
||
public class HealthController { | ||
|
||
private final HealthService healthService; | ||
|
||
public HealthController(final HealthService healthService) { | ||
this.healthService = healthService; | ||
} | ||
|
||
@GetMapping | ||
public ResponseEntity<List<HealthDTO>> getAllHealths() { | ||
return ResponseEntity.ok(healthService.findAll()); | ||
} | ||
|
||
@GetMapping("/{healthId}") | ||
public ResponseEntity<HealthDTO> getHealth( | ||
@PathVariable(name = "healthId") final Integer healthId) { | ||
return ResponseEntity.ok(healthService.get(healthId)); | ||
} | ||
|
||
@PostMapping | ||
@ApiResponse(responseCode = "201") | ||
public ResponseEntity<Integer> createHealth(@RequestBody @Valid final HealthDTO healthDTO) { | ||
final Integer createdHealthId = healthService.create(healthDTO); | ||
return new ResponseEntity<>(createdHealthId, HttpStatus.CREATED); | ||
} | ||
|
||
@PutMapping("/{healthId}") | ||
public ResponseEntity<Integer> updateHealth( | ||
@PathVariable(name = "healthId") final Integer healthId, | ||
@RequestBody @Valid final HealthDTO healthDTO) { | ||
healthService.update(healthId, healthDTO); | ||
return ResponseEntity.ok(healthId); | ||
} | ||
|
||
@DeleteMapping("/{healthId}") | ||
@ApiResponse(responseCode = "204") | ||
public ResponseEntity<Void> deleteHealth( | ||
@PathVariable(name = "healthId") final Integer healthId) { | ||
healthService.delete(healthId); | ||
return ResponseEntity.noContent().build(); | ||
} | ||
|
||
} |
34 changes: 34 additions & 0 deletions
34
src/main/java/com/ureca/sole_paradise/health/db/dto/HealthDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.ureca.sole_paradise.health.db.dto; | ||
|
||
import java.time.OffsetDateTime; | ||
|
||
import jakarta.validation.constraints.NotNull; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
@Getter | ||
@Setter | ||
public class HealthDTO { | ||
|
||
private Integer healthId; | ||
|
||
private OffsetDateTime visitedDate; | ||
|
||
@NotNull | ||
private String notes; | ||
|
||
private OffsetDateTime healthDate; | ||
|
||
private OffsetDateTime nextCheckupDate; | ||
|
||
@NotNull | ||
private OffsetDateTime createdAt; | ||
|
||
private OffsetDateTime updatedAt; | ||
|
||
private Boolean alarmStatus; | ||
|
||
@NotNull | ||
private Integer pet; | ||
|
||
} |
51 changes: 51 additions & 0 deletions
51
src/main/java/com/ureca/sole_paradise/health/db/entity/HealthEntity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package com.ureca.sole_paradise.health.db.entity; | ||
|
||
import java.time.OffsetDateTime; | ||
import com.ureca.sole_paradise.pet.db.entity.PetEntity; | ||
import jakarta.persistence.Column; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.FetchType; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.GenerationType; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.JoinColumn; | ||
import jakarta.persistence.ManyToOne; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
@Entity | ||
@Getter | ||
@Setter | ||
public class HealthEntity { | ||
|
||
@Id | ||
@Column(nullable = false, updatable = false) | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Integer healthId; | ||
|
||
@Column | ||
private OffsetDateTime visitedDate; | ||
|
||
@Column(nullable = false, columnDefinition = "longtext") | ||
private String notes; | ||
|
||
@Column | ||
private OffsetDateTime healthDate; | ||
|
||
@Column | ||
private OffsetDateTime nextCheckupDate; | ||
|
||
@Column(nullable = false) | ||
private OffsetDateTime createdAt; | ||
|
||
@Column | ||
private OffsetDateTime updatedAt; | ||
|
||
@Column(columnDefinition = "tinyint", length = 1) | ||
private Boolean alarmStatus; | ||
|
||
@ManyToOne(fetch = FetchType.LAZY) | ||
@JoinColumn(name = "pet_id", nullable = false) | ||
private PetEntity pet; | ||
|
||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/com/ureca/sole_paradise/health/db/repository/HealthRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.ureca.sole_paradise.health.db.repository; | ||
|
||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
import com.ureca.sole_paradise.health.db.entity.HealthEntity; | ||
import com.ureca.sole_paradise.pet.db.entity.PetEntity; | ||
|
||
|
||
public interface HealthRepository extends JpaRepository<HealthEntity, Integer> { | ||
|
||
HealthEntity findFirstByPet(PetEntity pet); | ||
|
||
} |
82 changes: 82 additions & 0 deletions
82
src/main/java/com/ureca/sole_paradise/health/service/HealthService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package com.ureca.sole_paradise.health.service; | ||
|
||
import java.util.List; | ||
import com.ureca.sole_paradise.util.NotFoundException; | ||
import org.springframework.data.domain.Sort; | ||
import org.springframework.stereotype.Service; | ||
import com.ureca.sole_paradise.health.db.dto.HealthDTO; | ||
import com.ureca.sole_paradise.health.db.entity.HealthEntity; | ||
import com.ureca.sole_paradise.health.db.repository.HealthRepository; | ||
import com.ureca.sole_paradise.pet.db.entity.PetEntity; | ||
import com.ureca.sole_paradise.pet.db.repository.PetRepository; | ||
|
||
@Service | ||
public class HealthService { | ||
|
||
private final HealthRepository healthRepository; | ||
private final PetRepository petRepository; | ||
|
||
public HealthService(final HealthRepository healthRepository, | ||
final PetRepository petRepository) { | ||
this.healthRepository = healthRepository; | ||
this.petRepository = petRepository; | ||
} | ||
|
||
public List<HealthDTO> findAll() { | ||
final List<HealthEntity> healths = healthRepository.findAll(Sort.by("healthId")); | ||
return healths.stream() | ||
.map(health -> mapToDTO(health, new HealthDTO())) | ||
.toList(); | ||
} | ||
|
||
public HealthDTO get(final Integer healthId) { | ||
return healthRepository.findById(healthId) | ||
.map(health -> mapToDTO(health, new HealthDTO())) | ||
.orElseThrow(NotFoundException::new); | ||
} | ||
|
||
public Integer create(final HealthDTO healthDTO) { | ||
final HealthEntity health = new HealthEntity(); | ||
mapToEntity(healthDTO, health); | ||
return healthRepository.save(health).getHealthId(); | ||
} | ||
|
||
public void update(final Integer healthId, final HealthDTO healthDTO) { | ||
final HealthEntity health = healthRepository.findById(healthId) | ||
.orElseThrow(NotFoundException::new); | ||
mapToEntity(healthDTO, health); | ||
healthRepository.save(health); | ||
} | ||
|
||
public void delete(final Integer healthId) { | ||
healthRepository.deleteById(healthId); | ||
} | ||
|
||
private HealthDTO mapToDTO(final HealthEntity health, final HealthDTO healthDTO) { | ||
healthDTO.setHealthId(health.getHealthId()); | ||
healthDTO.setVisitedDate(health.getVisitedDate()); | ||
healthDTO.setNotes(health.getNotes()); | ||
healthDTO.setHealthDate(health.getHealthDate()); | ||
healthDTO.setNextCheckupDate(health.getNextCheckupDate()); | ||
healthDTO.setCreatedAt(health.getCreatedAt()); | ||
healthDTO.setUpdatedAt(health.getUpdatedAt()); | ||
healthDTO.setAlarmStatus(health.getAlarmStatus()); | ||
healthDTO.setPet(health.getPet() == null ? null : health.getPet().getPetId()); | ||
return healthDTO; | ||
} | ||
|
||
private HealthEntity mapToEntity(final HealthDTO healthDTO, final HealthEntity health) { | ||
health.setVisitedDate(healthDTO.getVisitedDate()); | ||
health.setNotes(healthDTO.getNotes()); | ||
health.setHealthDate(healthDTO.getHealthDate()); | ||
health.setNextCheckupDate(healthDTO.getNextCheckupDate()); | ||
health.setCreatedAt(healthDTO.getCreatedAt()); | ||
health.setUpdatedAt(healthDTO.getUpdatedAt()); | ||
health.setAlarmStatus(healthDTO.getAlarmStatus()); | ||
final PetEntity pet = healthDTO.getPet() == null ? null : petRepository.findById(healthDTO.getPet()) | ||
.orElseThrow(() -> new NotFoundException("pet not found")); | ||
health.setPet(pet); | ||
return health; | ||
} | ||
|
||
} |
Oops, something went wrong.