Skip to content
2 changes: 2 additions & 0 deletions src/main/java/com/server/hispath/HispathApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;

@SpringBootApplication
@EnableJpaAuditing
public class HispathApplication {

public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
package com.server.hispath.activity.application;

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

import com.server.hispath.activity.application.dto.ActivityContentDto;
import com.server.hispath.activity.application.dto.ActivityDto;
import com.server.hispath.activity.domain.Activity;
import com.server.hispath.activity.domain.repository.ActivityRepository;
import com.server.hispath.category.application.CategoryService;
import com.server.hispath.category.domain.Category;
import com.server.hispath.exception.activity.ActivityNotFoundException;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import lombok.RequiredArgsConstructor;

Expand All @@ -11,5 +21,46 @@
public class ActivityService {

private final ActivityRepository activityRepository;
private final CategoryService categoryService;

@Transactional
public Long create(Long categoryId, ActivityContentDto dto) {
Category category = categoryService.findById(categoryId);
Activity activity = Activity.from(category, dto);
Activity savedActivity = activityRepository.save(activity);
return savedActivity.getId();
}
@Transactional(readOnly = true)
public ActivityDto find(Long id) {
Activity activity = this.findById(id);
return ActivityDto.from(activity);
}



@Transactional(readOnly = true)
public List<ActivityDto> findAll() {
List<Activity> activities = activityRepository.findAll();
return activities.stream()
.map(ActivityDto::from)
.collect(Collectors.toList());
}

@Transactional
public ActivityDto update(Long id, Long categoryId, ActivityContentDto dto){
Activity activity = this.findById(id);
Category category = categoryService.findById(categoryId);
activity.update(category, dto);

return ActivityDto.from(activity);
}

@Transactional
public void delete(Long id){
activityRepository.deleteById(id);
}

private Activity findById(Long id) {
return activityRepository.findById(id).orElseThrow(ActivityNotFoundException::new);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.server.hispath.activity.application.dto;

import com.server.hispath.activity.presentation.request.ActivityCURequest;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class ActivityContentDto {

private String semester;
private boolean personal;
private int requestStatus;
private String data;

public static ActivityContentDto from(ActivityCURequest request){
return new ActivityContentDto(request.getSemester(), request.isPersonal(), request.getRequestStatus(), request.getData());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.server.hispath.activity.application.dto;

import com.server.hispath.activity.domain.Activity;
import com.server.hispath.category.application.dto.CategoryDto;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor
@AllArgsConstructor
public class ActivityDto {
private Long id;
private CategoryDto categoryDto;
private String semester;
private boolean personal;
private String data;

public static ActivityDto from(Activity activity) {
return new ActivityDto(activity.getId(), CategoryDto.from(activity.getCategory()),
activity.getSemester(), activity.isPersonal(), activity.getData());
}
}
30 changes: 28 additions & 2 deletions src/main/java/com/server/hispath/activity/domain/Activity.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,24 @@
import java.util.List;
import javax.persistence.*;

import com.server.hispath.activity.application.dto.ActivityContentDto;
import com.server.hispath.category.domain.Category;
import com.server.hispath.common.BaseEntity;
import com.server.hispath.student.domain.participate.Participant;
import com.sun.istack.NotNull;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;

@Entity
@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Where(clause = "deleted = false")
@SQLDelete(sql = "UPDATE activity SET deleted = true Where id = ?")
public class Activity extends BaseEntity {
Expand All @@ -27,14 +33,34 @@ public class Activity extends BaseEntity {
@ManyToOne(fetch = FetchType.LAZY)
private Category category;

@NotNull
private String semester;

private boolean personel;
private boolean personal;

private int requestStatus;

private String date;
@NotNull
private String data;

@OneToMany(mappedBy = "activity")
private List<Participant> participants = new ArrayList<>();

public static Activity from(Category category, ActivityContentDto dto) {
return Activity.builder()
.category(category)
.semester(dto.getSemester())
.personal(dto.isPersonal())
.requestStatus(dto.getRequestStatus())
.data(dto.getData())
.build();
}

public void update(Category category, ActivityContentDto dto){
this.category = category;
this.semester = dto.getSemester();
this.personal = dto.isPersonal();
this.requestStatus = dto.getRequestStatus();
this.data = dto.getData();
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,69 @@
package com.server.hispath.activity.presentation;

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

import com.server.hispath.activity.application.ActivityService;
import com.server.hispath.activity.application.dto.ActivityContentDto;
import com.server.hispath.activity.application.dto.ActivityDto;
import com.server.hispath.activity.presentation.request.ActivityCURequest;
import com.server.hispath.activity.presentation.response.ActivityResponse;
import com.server.hispath.docs.ApiDoc;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.ApiOperation;

import lombok.RequiredArgsConstructor;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api")
public class ActivityController {
private final ActivityService activityService;

@PostMapping("/activity")
@ApiOperation(value = ApiDoc.ACTIVITY_CREATE)
public ResponseEntity<Long> create(@RequestBody ActivityCURequest request) {
Long id = activityService.create(request.getCategoryId(), ActivityContentDto.from(request));
return ResponseEntity.ok(id);
}

@GetMapping("/activity/{id}")
@ApiOperation(value = ApiDoc.ACTIVITY_READ)
public ResponseEntity<ActivityResponse> find(@PathVariable Long id) {

ActivityResponse response = ActivityResponse.from(activityService.find(id));
return ResponseEntity.ok(response);

}

@GetMapping("/activities")
@ApiOperation(value = ApiDoc.ACTIVITY_READ_ALL)
public ResponseEntity<List<ActivityResponse>> findAll() {

List<ActivityResponse> responses = activityService.findAll()
.stream()
.map(ActivityResponse::from)
.collect(Collectors.toList());
return ResponseEntity.ok(responses);

}

@PatchMapping("/activity/{id}")
@ApiOperation(value = ApiDoc.ACTIVITY_UPDATE)
public ResponseEntity<ActivityResponse> update(@PathVariable Long id, @RequestBody ActivityCURequest request) {
ActivityDto dto = activityService.update(id, request.getCategoryId(), ActivityContentDto.from(request));
ActivityResponse response = ActivityResponse.from(dto);

return ResponseEntity.ok(response);
}

@DeleteMapping("/activity/{id}")
@ApiOperation(value = ApiDoc.ACTIVITY_DELETE)
public ResponseEntity<Long> delete(@PathVariable Long id) {
activityService.delete(id);
return ResponseEntity.ok(id);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.server.hispath.activity.presentation.request;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Getter
@Setter
@NoArgsConstructor
public class ActivityCURequest {
private Long categoryId;
private String semester;
private boolean personal;
private int requestStatus;
private String data;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.server.hispath.activity.presentation.response;

import com.server.hispath.activity.application.dto.ActivityDto;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Getter
@NoArgsConstructor
@AllArgsConstructor
public class ActivityResponse {
private Long id;
private Long categoryId;
private String categoryName;
private String semester;
private boolean personal;
private String data;

public static ActivityResponse from(ActivityDto dto) {
return new ActivityResponse(dto.getId(), dto.getCategoryDto().getId(), dto.getCategoryDto().getName(),
dto.getSemester(), dto.isPersonal(), dto.getData());
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
package com.server.hispath.category.application;

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

import com.server.hispath.category.application.dto.CategoryContentDto;
import com.server.hispath.category.application.dto.CategoryCUDto;
import com.server.hispath.category.domain.Category;
import com.server.hispath.category.domain.repository.CategoryRepository;
import com.server.hispath.exception.category.CategoryNotFoundException;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import lombok.RequiredArgsConstructor;

Expand All @@ -12,4 +20,39 @@ public class CategoryService {

private final CategoryRepository categoryRepository;

@Transactional
public Long create(CategoryCUDto dto) {
Category savedCategory = categoryRepository.save(Category.from(dto));
return savedCategory.getId();
}

@Transactional(readOnly = true)
public CategoryContentDto find(Long id) {
Category category = this.findById(id);
return CategoryContentDto.from(category);
}

@Transactional(readOnly = true)
public List<CategoryContentDto> findAll() {
List<Category> categories = categoryRepository.findAll();
return categories.stream()
.map(CategoryContentDto::from)
.collect(Collectors.toList());
}

@Transactional
public CategoryContentDto update(Long id, CategoryCUDto dto){
Category category = categoryRepository.findById(id).orElseThrow(CategoryNotFoundException::new);
category.update(dto);
return CategoryContentDto.from(category);
}

@Transactional
public void delete(Long id){
categoryRepository.deleteById(id);
}

public Category findById(Long id){
return categoryRepository.findById(id).orElseThrow(CategoryNotFoundException::new);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.server.hispath.category.application.dto;

import com.server.hispath.category.domain.ActivityType;
import com.server.hispath.category.presentation.request.CategoryCURequest;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor
@AllArgsConstructor
public class CategoryCUDto {
private ActivityType type;
private String name;

public static CategoryCUDto of(CategoryCURequest request) {
return new CategoryCUDto(ActivityType.valueOf(request.getType()), request.getName());
}
}
Loading