Skip to content

Commit

Permalink
Merge pull request #45 from Informatik-Projekt-Kurs/IPK-167-Backend-C…
Browse files Browse the repository at this point in the history
…ompany-Setup

Ipk 167 backend company setup
  • Loading branch information
bencodes07 authored Jun 22, 2024
2 parents e9efb0e + 1650117 commit 7b46f38
Show file tree
Hide file tree
Showing 9 changed files with 136 additions and 25 deletions.
39 changes: 39 additions & 0 deletions src/main/java/com/MeetMate/_experiments/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#Test Mongo Database
version: '3'

services:
mongodb:
image: mongo:latest
container_name: mongodb
restart: always
ports:
- "27017:27017"
networks:
- mongo-net
volumes:
- data:/data
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: pass

mongo-express:
image: mongo-express
container_name: mongo-express
restart: always
ports:
- "8082:8081"
networks:
- mongo-net
depends_on:
- mongodb
environment:
ME_CONFIG_MONGODB_ADMINUSERNAME: root
ME_CONFIG_MONGODB_ADMINPASSWORD: pass
ME_CONFIG_MONGODB_SERVER: mongodb

volumes:
data: { }

networks:
mongo-net:
driver: bridge
4 changes: 3 additions & 1 deletion src/main/java/com/MeetMate/company/Company.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@
@Data
@NoArgsConstructor
public class Company {
private long id;
private String name;
private String description;
private BusinessType businessType;
private String[] memberEmails;
@Indexed(unique = true)
private String ownerEmail;

public Company(String name, String ownerEmail) {
public Company(long id,String name, String ownerEmail) {
this.id = id;
this.name = name;
this.ownerEmail = ownerEmail;
this.description = "";
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/com/MeetMate/company/CompanyController.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,9 @@ public class CompanyController {
private final CompanyService companyService;

@QueryMapping
public Company getCompany(@ContextValue(name = "token") String token) {
token = token.substring(7);
public Company getCompany(@Argument long id) {
try {
return companyService.getCompany(token);
return companyService.getCompany(id);

} catch (Throwable t) {
Class<? extends Throwable> tc = t.getClass();
Expand Down
7 changes: 1 addition & 6 deletions src/main/java/com/MeetMate/company/CompanyRepository.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -13,7 +10,5 @@ public interface CompanyRepository extends MongoRepository<Company, Long> {

Optional<Company> findCompanyByOwnerEmail(String ownerEmail);

// @Modifying
// @Query("{ 'ownerEmail': :#{#ownerEmail} }")
// Company updateByOwnerEmail(String ownerEmail);
Optional<Company> findCompanyById(long id);
}
35 changes: 21 additions & 14 deletions src/main/java/com/MeetMate/company/CompanyService.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.MeetMate.company;

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;
Expand All @@ -23,19 +23,18 @@ 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;

public Company getCompany(String token) throws IllegalArgumentException {
String ownerEmail = jwtService.extractUserEmail(token);
public Company getCompany(long id) throws IllegalArgumentException {

//Test if user is a company owner
if (userRepository.findUserByEmail(ownerEmail)
if (userRepository.findUserById(id)
.orElseThrow(() -> new EntityNotFoundException("User not found!"))
.getRole() != UserRole.COMPANY_OWNER)
throw new IllegalArgumentException("User is not a company owner");

return companyRepository.findCompanyByOwnerEmail(ownerEmail)
return companyRepository.findCompanyById(id)
.orElseThrow(() -> new EntityNotFoundException("Company not found"));
}

Expand All @@ -50,17 +49,13 @@ public void createCompany(String companyName, String ownerEmail, String ownerNam

userController.registerNewUser(ownerData);

companyRepository.save(new Company(companyName, ownerEmail));
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 = getCompany(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();
Expand All @@ -72,12 +67,24 @@ public void editCompany(String token, String companyName, String description, St

@Transactional
public void deleteCompany(String token) {
Company company = getCompany(token);
Company company = getCompanyWithToken(token);
try {
userController.deleteUser(token);
} catch (Throwable t) {
return;
}
companyRepository.delete(company);
}

private Company getCompanyWithToken(String ownerEmail) throws IllegalArgumentException {

//Test if user is a company owner
if (userRepository.findUserByEmail(ownerEmail)
.orElseThrow(() -> new EntityNotFoundException("User not found!"))
.getRole() != UserRole.COMPANY_OWNER)
throw new IllegalArgumentException("User is not a company owner");

return companyRepository.findCompanyByOwnerEmail(ownerEmail)
.orElseThrow(() -> new EntityNotFoundException("Company not found"));
}
}
16 changes: 16 additions & 0 deletions src/main/java/com/MeetMate/company/sequence/CompanySequence.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.MeetMate.company.sequence;

import lombok.AllArgsConstructor;
import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

@Document(collection = "company_sequence")
@Data
@AllArgsConstructor
public class CompanySequence {
@Id
String id;
long value;

}
20 changes: 20 additions & 0 deletions src/main/java/com/MeetMate/company/sequence/SequenceConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.MeetMate.company.sequence;

import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.core.MongoTemplate;

@Configuration
public class SequenceConfig {

@Bean
public CommandLineRunner init(MongoTemplate mongoTemplate) {
return args -> {
if (!mongoTemplate.collectionExists(CompanySequence.class)) {
mongoTemplate.createCollection(CompanySequence.class);
mongoTemplate.insert(new CompanySequence("company_sequence", 0));
}
};
}
}
33 changes: 33 additions & 0 deletions src/main/java/com/MeetMate/company/sequence/SequenceService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.MeetMate.company.sequence;

import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class SequenceService {

private final MongoTemplate mongoTemplate;

@Transactional
public void incrementId() {
Query query = new Query(Criteria.where("_id").is("company_sequence"));
Update update = new Update().inc("value", 1);
mongoTemplate.updateFirst(query, update, CompanySequence.class);
}

public long getAndIncrementCurrentValue(){
Query query = new Query(Criteria.where("_id").is("company_sequence"));
CompanySequence sequence = mongoTemplate.findOne(query, CompanySequence.class);
long value = sequence.getValue();
incrementId();
return value;
}


}
2 changes: 1 addition & 1 deletion src/main/resources/graphql/schema.graphqls
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#graphqls for graphql scema

type Query {
getCompany: Company
getCompany(id: ID!): Company
}

type Mutation {
Expand Down

0 comments on commit 7b46f38

Please sign in to comment.