From 1650117c95744c2d47590c468eaf064aeb6d4b56 Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 20 Jun 2024 21:03:48 +0200 Subject: [PATCH] Changed Company fetching from access token to id and added own id sequence for companies --- src/main/java/com/MeetMate/company/Company.java | 1 - .../java/com/MeetMate/company/CompanyRepository.java | 7 ------- .../java/com/MeetMate/company/CompanyService.java | 12 ++---------- .../MeetMate/company/sequence/SequenceService.java | 2 +- 4 files changed, 3 insertions(+), 19 deletions(-) diff --git a/src/main/java/com/MeetMate/company/Company.java b/src/main/java/com/MeetMate/company/Company.java index 0d9dffa..52e7a24 100644 --- a/src/main/java/com/MeetMate/company/Company.java +++ b/src/main/java/com/MeetMate/company/Company.java @@ -3,7 +3,6 @@ import com.MeetMate.enums.BusinessType; import lombok.Data; import lombok.NoArgsConstructor; -import org.springframework.data.annotation.Id; import org.springframework.data.mongodb.core.index.Indexed; import org.springframework.data.mongodb.core.mapping.Document; diff --git a/src/main/java/com/MeetMate/company/CompanyRepository.java b/src/main/java/com/MeetMate/company/CompanyRepository.java index 79db6d8..1859aab 100644 --- a/src/main/java/com/MeetMate/company/CompanyRepository.java +++ b/src/main/java/com/MeetMate/company/CompanyRepository.java @@ -1,9 +1,6 @@ package com.MeetMate.company; -import org.springframework.data.jpa.repository.Modifying; import org.springframework.data.mongodb.repository.MongoRepository; -import org.springframework.data.mongodb.repository.Query; -import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Repository; import java.util.Optional; @@ -14,8 +11,4 @@ public interface CompanyRepository extends MongoRepository { Optional findCompanyByOwnerEmail(String ownerEmail); Optional findCompanyById(long id); - -// @Modifying -// @Query("{ 'ownerEmail': :#{#ownerEmail} }") -// Company updateByOwnerEmail(String ownerEmail); } diff --git a/src/main/java/com/MeetMate/company/CompanyService.java b/src/main/java/com/MeetMate/company/CompanyService.java index a33b4e1..48e631e 100644 --- a/src/main/java/com/MeetMate/company/CompanyService.java +++ b/src/main/java/com/MeetMate/company/CompanyService.java @@ -1,10 +1,8 @@ package com.MeetMate.company; -import com.MeetMate.company.sequence.CompanySequence; import com.MeetMate.company.sequence.SequenceService; import com.MeetMate.enums.BusinessType; import com.MeetMate.enums.UserRole; -import com.MeetMate.security.JwtService; import com.MeetMate.user.UserController; import com.MeetMate.user.UserRepository; import jakarta.persistence.EntityNotFoundException; @@ -25,7 +23,6 @@ public class CompanyService { private final UserController userController; private final UserRepository userRepository; private final CompanyRepository companyRepository; - private final JwtService jwtService; private final MongoTemplate mongoTemplate; private final SequenceService sequenceService; @@ -52,18 +49,13 @@ public void createCompany(String companyName, String ownerEmail, String ownerNam userController.registerNewUser(ownerData); - long companyId = sequenceService.getCurrentValue(); + long companyId = sequenceService.getAndIncrementCurrentValue(); companyRepository.save(new Company(companyId, companyName, ownerEmail)); } @Transactional public void editCompany(String token, String companyName, String description, String businessType) { - Company company = getCompanyWithToken(token); - String ownerEmail = jwtService.extractUserEmail(token); -// if(companyName != null) company.setName(companyName); -// if(description != null) company.setDescription(description); -// if(businessType != null) company.setBusinessType(BusinessType.valueOf(businessType)); -// companyRepository.save(company); + String ownerEmail = getCompanyWithToken(token).getOwnerEmail(); Query query = new Query(Criteria.where("ownerEmail").is(ownerEmail)); Update update = new Update(); diff --git a/src/main/java/com/MeetMate/company/sequence/SequenceService.java b/src/main/java/com/MeetMate/company/sequence/SequenceService.java index 48ee764..f245bdb 100644 --- a/src/main/java/com/MeetMate/company/sequence/SequenceService.java +++ b/src/main/java/com/MeetMate/company/sequence/SequenceService.java @@ -21,7 +21,7 @@ public void incrementId() { mongoTemplate.updateFirst(query, update, CompanySequence.class); } - public long getCurrentValue(){ + public long getAndIncrementCurrentValue(){ Query query = new Query(Criteria.where("_id").is("company_sequence")); CompanySequence sequence = mongoTemplate.findOne(query, CompanySequence.class); long value = sequence.getValue();