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

✨ feat: 내 조직 조회 / 수정 / 삭제 기능 개발 #55

Merged
merged 8 commits into from
Jan 29, 2024
38 changes: 38 additions & 0 deletions http/test.http
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,41 @@ Content-Type: application/json

### 보고서 조회
GET http://localhost:8080/api/v1/reports/0

### 내 조직 조회
GET http://localhost:8080/api/v1/organizations/me
Authorization: Bearer {{matsterToken}}

### 내 조직 수정
PATCH http://localhost:8080/api/v1/organizations/me
Authorization: Bearer {{matsterToken}}
Content-Type: application/json

{
"name": "팀 스포너스 업데이트!!!!",
"email": "[email protected] 업데이트!!!!",
"password": "password1234 업데이트!!!!",
"location": "none 업데이트!!!!",
"organizationType": "COMPANY",
"suborganizationType": "CLUB",
"managerName": "이가은",
"managerPosition": "Project Manager",
"managerEmail": "[email protected]",
"managerPhone": "01012345678",
"managerAvailableDay": "월-금",
"managerAvailableHour": "15:00-18:00",
"managerContactPreference": "EMAIL 업데이트!!!!"
}

### 내 조직 수정 2
PATCH http://localhost:8080/api/v1/organizations/me
Authorization: Bearer {{matsterToken}}
Content-Type: application/json

{
"name": "팀 스포너스 업데이트!!!!2"
}

### 내 조직 삭제 [soft delete] (OrganizationStatus = INACTIVE)
DELETE http://localhost:8080/api/v1/organizations/me
Authorization: Bearer {{matsterToken}}
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package com.sponus.sponusbe.domain.organization.controller;

import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.sponus.sponusbe.auth.annotation.AuthOrganization;
import com.sponus.sponusbe.domain.organization.dto.OrganizationDetailGetResponse;
import com.sponus.sponusbe.domain.organization.dto.OrganizationJoinRequest;
import com.sponus.sponusbe.domain.organization.dto.OrganizationJoinResponse;
import com.sponus.sponusbe.domain.organization.dto.OrganizationUpdateRequest;
import com.sponus.sponusbe.domain.organization.entity.Organization;
import com.sponus.sponusbe.domain.organization.service.OrganizationService;
import com.sponus.sponusbe.global.common.ApiResponse;
Expand All @@ -34,4 +38,24 @@ public ApiResponse<Long> test(@AuthOrganization Organization organization) {
Long id = organization.getId();
return ApiResponse.onSuccess(id);
}

@GetMapping("/me")
public ApiResponse<OrganizationDetailGetResponse> getMyOrganization(@AuthOrganization Organization organization) {
return ApiResponse.onSuccess(OrganizationDetailGetResponse.from(organization));
}

@PatchMapping("/me")
public ApiResponse<Void> updateMyOrganization(
@AuthOrganization Organization organization,
@RequestBody @Valid OrganizationUpdateRequest request
) {
organizationService.updateOrganization(organization.getId(), request);
return ApiResponse.onSuccess(null);
}

@DeleteMapping("/me")
public ApiResponse<Void> deleteMyOrganization(@AuthOrganization Organization organization) {
organizationService.deactivateOrganization(organization.getId());
return ApiResponse.onSuccess(null);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.sponus.sponusbe.domain.organization.dto;

import com.sponus.sponusbe.domain.organization.entity.Organization;
import com.sponus.sponusbe.domain.organization.entity.enums.OrganizationStatus;
import com.sponus.sponusbe.domain.organization.entity.enums.OrganizationType;
import com.sponus.sponusbe.domain.organization.entity.enums.SuborganizationType;

import java.util.List;

public record OrganizationDetailGetResponse(
Long organizationId,
String name,
String email,
String password,
String location,
String description,
String imageUrl,
OrganizationType organizationType,
SuborganizationType suborganizationType,
String managerName,
String managerPosition,
String managerEmail,
String managerPhone,
String managerAvailableDay,
String managerAvailableHour,
String managerContactPreference,
OrganizationStatus organizationStatus,
List<TagGetResponse> tags,
List<OrganizationLinkGetResponse> links
) {
public static OrganizationDetailGetResponse from(Organization organization) {
List<TagGetResponse> tagGetResponses = TagGetResponse.getTagResponses(organization);
List<OrganizationLinkGetResponse> linkGetResponses = OrganizationLinkGetResponse.getOrganizationLinkResponses(organization);
return new OrganizationDetailGetResponse(
organization.getId(),
organization.getName(),
organization.getEmail(),
organization.getPassword(),
organization.getLocation(),
organization.getDescription(),
organization.getImageUrl(),
organization.getOrganizationType(),
organization.getSuborganizationType(),
organization.getManagerName(),
organization.getManagerPosition(),
organization.getManagerEmail(),
organization.getManagerPhone(),
organization.getManagerAvailableDay(),
organization.getManagerAvailableHour(),
organization.getManagerContactPreference(),
organization.getOrganizationStatus(),
tagGetResponses,
linkGetResponses
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.sponus.sponusbe.domain.organization.dto;

import com.sponus.sponusbe.domain.organization.entity.Organization;
import com.sponus.sponusbe.domain.organization.entity.OrganizationLink;

import java.util.List;

public record OrganizationLinkGetResponse(
Long organizationLinkId,
Long organizationId,
String name,
String url
) {
public static OrganizationLinkGetResponse from(OrganizationLink organizationLink) {
return new OrganizationLinkGetResponse(
organizationLink.getId(),
organizationLink.getOrganization().getId(),
organizationLink.getName(),
organizationLink.getUrl()
);
}

public static List<OrganizationLinkGetResponse> getOrganizationLinkResponses(Organization organization) {
return organization.getOrganizationLinks().stream()
.map(OrganizationLinkGetResponse::from)
.toList();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.sponus.sponusbe.domain.organization.dto;

import com.sponus.sponusbe.domain.organization.entity.enums.OrganizationType;
import com.sponus.sponusbe.domain.organization.entity.enums.SuborganizationType;

public record OrganizationUpdateRequest(
String name,
String email,
String password,
String location,
String description,
String imageUrl,
OrganizationType organizationType,
SuborganizationType suborganizationType,
String managerName,
String managerPosition,
String managerEmail,
String managerPhone,
String managerAvailableDay,
String managerAvailableHour,
String managerContactPreference
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.sponus.sponusbe.domain.organization.dto;

import com.sponus.sponusbe.domain.organization.entity.Organization;
import com.sponus.sponusbe.domain.organization.entity.Tag;

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

public record TagGetResponse(
Long id,
String name
) {
public static TagGetResponse from(Tag tag) {
return new TagGetResponse(tag.getId(), tag.getName());
}

public static List<TagGetResponse> getTagResponses(Organization organization) {
return organization.getOrganizationTags()
.stream()
.map(organizationTag -> TagGetResponse.from(organizationTag.getTag()))
.toList();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.ArrayList;
import java.util.List;

import com.sponus.sponusbe.domain.organization.dto.OrganizationUpdateRequest;
import com.sponus.sponusbe.domain.organization.entity.enums.OrganizationStatus;
import com.sponus.sponusbe.domain.organization.entity.enums.OrganizationType;
import com.sponus.sponusbe.domain.organization.entity.enums.SuborganizationType;
Expand All @@ -12,6 +13,7 @@
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
Expand Down Expand Up @@ -88,14 +90,44 @@ public class Organization extends BaseEntity {
private OrganizationStatus organizationStatus;

@Builder.Default
@OneToMany(mappedBy = "organization")
@OneToMany(mappedBy = "organization", fetch = FetchType.EAGER)
private List<OrganizationTag> organizationTags = new ArrayList<>();

@Builder.Default
@OneToMany(mappedBy = "organization")
@OneToMany(mappedBy = "organization", fetch = FetchType.EAGER)
private List<OrganizationLink> organizationLinks = new ArrayList<>();

public boolean isStudentOrganization() {
return this.organizationType == OrganizationType.STUDENT;
}

public void update(OrganizationUpdateRequest request) {
this.name = request.name() == null ? this.name : request.name();
this.email = request.email() == null ? this.email : request.email();
this.password = request.password() == null ? this.password : request.password();
this.location = request.location() == null ? this.location : request.location();
this.description = request.description() == null ? this.description : request.description();
this.imageUrl = request.imageUrl() == null ? this.imageUrl : request.imageUrl();
this.organizationType = request.organizationType() == null ? this.organizationType : request.organizationType();
this.suborganizationType =
request.suborganizationType() == null ? this.suborganizationType : request.suborganizationType();
this.managerName = request.managerName() == null ? this.managerName : request.managerName();
this.managerPosition = request.managerPosition() == null ? this.managerPosition : request.managerPosition();
this.managerEmail = request.managerEmail() == null ? this.managerEmail : request.managerEmail();
this.managerPhone = request.managerPhone() == null ? this.managerPhone : request.managerPhone();
this.managerAvailableDay =
request.managerAvailableDay() == null ? this.managerAvailableDay : request.managerAvailableDay();
this.managerAvailableHour =
request.managerAvailableHour() == null ? this.managerAvailableHour : request.managerAvailableHour();
this.managerContactPreference = request.managerContactPreference() == null ? this.managerContactPreference :
request.managerContactPreference();
}

public void deactivate() {
this.organizationStatus = OrganizationStatus.INACTIVE;
}

public void activate() {
this.organizationStatus = OrganizationStatus.ACTIVE;
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package com.sponus.sponusbe.domain.organization.service;

import static com.sponus.sponusbe.domain.organization.exception.OrganizationErrorCode.*;

import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sponus.sponusbe.domain.organization.dto.OrganizationJoinRequest;
import com.sponus.sponusbe.domain.organization.dto.OrganizationJoinResponse;
import com.sponus.sponusbe.domain.organization.dto.OrganizationUpdateRequest;
import com.sponus.sponusbe.domain.organization.entity.Organization;
import com.sponus.sponusbe.domain.organization.exception.OrganizationException;
import com.sponus.sponusbe.domain.organization.repository.OrganizationRepository;

import lombok.RequiredArgsConstructor;
Expand All @@ -25,4 +29,18 @@ public OrganizationJoinResponse join(OrganizationJoinRequest request) {
request.toEntity(passwordEncoder.encode(request.password())));
return OrganizationJoinResponse.from(organization);
}

@Transactional
public void updateOrganization(Long organizationId, OrganizationUpdateRequest request) {
Organization organization = organizationRepository.findById(organizationId)
.orElseThrow(() -> new OrganizationException(ORGANIZATION_NOT_FOUND));
organization.update(request);
}

@Transactional
public void deactivateOrganization(Long organizationId) {
Organization organization = organizationRepository.findById(organizationId)
.orElseThrow(() -> new OrganizationException(ORGANIZATION_NOT_FOUND));
organization.deactivate();
}
}
Loading