Skip to content
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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,7 @@ out/

### VS Code ###
.vscode/

### PostMan ###
.postman/
postman/
13 changes: 13 additions & 0 deletions .postman/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"workspace": {
"id": "9773296a-87d0-4545-b261-b2bb4026cca9"
},
"entities": {
"environments": [],
"flows": [],
"globals": [],
"mocks": [],
"specs": [],
"collections": []
}
}
7 changes: 7 additions & 0 deletions postman/globals/workspace.postman_globals.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"id": "a141368c-2e88-4434-9b5b-5ee59046fed2",
"name": "Globals",
"values": [],
"_postman_variable_scope": "globals",
"_postman_exported_at": "2026-06-28T05:43:38.225Z"
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,42 @@
package com.github.minuk1749.workout_tracker.controller;

import com.github.minuk1749.workout_tracker.domain.WeightRecord;
import com.github.minuk1749.workout_tracker.dto.WeightRecordRequest;
import com.github.minuk1749.workout_tracker.dto.WeightRecordResponse;
import com.github.minuk1749.workout_tracker.service.WeightRecordService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.PatchMapping;

import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/weight")
@RequestMapping("/api/weights")
public class WeightRecordController {

private final WeightRecordService weightRecordService;

@GetMapping
public List<WeightRecord> getAllWeightRecords() {
return weightRecordService.getAllRecords();
public List<WeightRecordResponse> getAllWeightRecords() {
return weightRecordService.getAllWeightRecords();
}

@PostMapping
public WeightRecordResponse saveWeightRecord(@RequestBody WeightRecordRequest request) { // POST 기능
return weightRecordService.saveWeightRecord(request);
}

@DeleteMapping("/{id}")
public void deleteWeightRecord(@PathVariable Long id) { // DELETE 기능
weightRecordService.deleteWeightRecord(id);
}

@PatchMapping("/{id}")
public WeightRecordResponse updateWeightRecord(@PathVariable Long id, @RequestBody WeightRecordRequest request) {
return weightRecordService.updateWeightRecord(id, request);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,18 @@ public class WeightRecord {
private Long id;

@Column(nullable = false)
private Double weight;
private Double weight; //체중

@Column(nullable = false)
private LocalDate recordedAt;
private LocalDate recordedAt; // 기록한 일시

public void update(Double weight, LocalDate recordedAt) {
this.weight = weight;
this.recordedAt = recordedAt;
}

public WeightRecord(Double weight, LocalDate recordedAt) {
this.weight = weight;
this.recordedAt = recordedAt;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.github.minuk1749.workout_tracker.dto;

import lombok.Getter;
import lombok.NoArgsConstructor;
import java.time.LocalDate;

@Getter
@NoArgsConstructor
public class WeightRecordRequest {
private Double weight;
private LocalDate recordedAt;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.github.minuk1749.workout_tracker.dto;

import com.github.minuk1749.workout_tracker.domain.WeightRecord;
import lombok.Getter;

import java.time.LocalDate;

@Getter
public class WeightRecordResponse {

private final Long id;
private final Double weight;
private final LocalDate recordedAt;
/* 이 세개의 변수는 현재는 WeightRecord에 있는 변수 전부이지만
만약 WeightRecord에 변수가 여러개 만들어졌다면 그 중 원하는 것만 골라서 적으면 됨 */

public WeightRecordResponse(WeightRecord weightRecord) {
this.id = weightRecord.getId();
this.weight = weightRecord.getWeight();
this.recordedAt = weightRecord.getRecordedAt();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
import org.springframework.data.jpa.repository.JpaRepository;

public interface WeightRecordRepository extends JpaRepository<WeightRecord, Long> {

//JpaRepository를 상속받기만 해도 저장,조회,삭제기능이 자동으로 생김
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,45 @@
package com.github.minuk1749.workout_tracker.service;

import com.github.minuk1749.workout_tracker.dto.WeightRecordRequest;
import com.github.minuk1749.workout_tracker.domain.WeightRecord;
import com.github.minuk1749.workout_tracker.dto.WeightRecordRequest;
import com.github.minuk1749.workout_tracker.dto.WeightRecordResponse;
import com.github.minuk1749.workout_tracker.repository.WeightRecordRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class WeightRecordService {

private final WeightRecordRepository weightRecordRepository;

public List<WeightRecord> getAllRecords() {
return weightRecordRepository.findAll();
} // DB에 있는 체중기록 전부를 불러옴
public List<WeightRecordResponse> getAllWeightRecords () { //GET 기능
return weightRecordRepository.findAll()
.stream()
.map(WeightRecordResponse::new)
.collect(Collectors.toList());
}

public WeightRecordResponse saveWeightRecord(WeightRecordRequest request) { // POST 기능
WeightRecord weightRecord = new WeightRecord(request.getWeight(), request.getRecordedAt());
WeightRecord saved = weightRecordRepository.save(weightRecord); // 기본으로 있는 save기능 활용
return new WeightRecordResponse(saved);
}

public void deleteWeightRecord(Long id) { //DELETE 기능
weightRecordRepository.deleteById(id);
}

@Transactional
public WeightRecordResponse updateWeightRecord(Long id, WeightRecordRequest request) { // PATCH 기능
WeightRecord weightRecord = weightRecordRepository.findById(id)
.orElseThrow(() -> new RuntimeException("체중 기록을 찾을 수 없습니다"));
weightRecord.update(request.getWeight(), request.getRecordedAt());
return new WeightRecordResponse(weightRecord);
}
}