expense List logic
This commit is contained in:
@@ -1,38 +0,0 @@
|
||||
package de.zendric.app.XpenselyServer.controller;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
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 de.zendric.app.XpenselyServer.model.ExpenseList;
|
||||
import de.zendric.app.XpenselyServer.services.ExpenseListService;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/lists")
|
||||
public class ExpenseListController {
|
||||
|
||||
@Autowired
|
||||
private ExpenseListService service;
|
||||
|
||||
@GetMapping
|
||||
public List<ExpenseList> getAllLists() {
|
||||
return service.getAllLists();
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
public ExpenseList createList(@RequestBody ExpenseList list) {
|
||||
return service.createList(list);
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
public void deleteList(@PathVariable Long id) {
|
||||
service.deleteList(id);
|
||||
}
|
||||
}
|
||||
@@ -1,26 +0,0 @@
|
||||
package de.zendric.app.XpenselyServer.model;
|
||||
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.GenerationType;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.ManyToOne;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@Entity
|
||||
public class Expense {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
private String description;
|
||||
private Double amount;
|
||||
|
||||
|
||||
@ManyToOne
|
||||
private ExpenseList expenseList;
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
package de.zendric.app.XpenselyServer.security;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||
import org.springframework.security.web.SecurityFilterChain;
|
||||
|
||||
@Configuration
|
||||
public class SecurityConfig {
|
||||
|
||||
@Bean
|
||||
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
|
||||
http
|
||||
.csrf().disable() // Disable CSRF for testing
|
||||
.authorizeHttpRequests()
|
||||
.anyRequest().permitAll(); // Allow all requests
|
||||
return http.build();
|
||||
}
|
||||
}
|
||||
@@ -1,27 +0,0 @@
|
||||
package de.zendric.app.XpenselyServer.services;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import de.zendric.app.XpenselyServer.model.ExpenseList;
|
||||
import de.zendric.app.XpenselyServer.repo.ExpenseListRepository;
|
||||
|
||||
@Service
|
||||
public class ExpenseListService {
|
||||
@Autowired
|
||||
private ExpenseListRepository repository;
|
||||
|
||||
public List<ExpenseList> getAllLists() {
|
||||
return repository.findAll();
|
||||
}
|
||||
|
||||
public ExpenseList createList(ExpenseList list) {
|
||||
return repository.save(list);
|
||||
}
|
||||
|
||||
public void deleteList(Long id) {
|
||||
repository.deleteById(id);
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package de.zendric.app.XpenselyServer;
|
||||
package de.zendric.app.xpensely_server;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
@@ -1,4 +1,4 @@
|
||||
package de.zendric.app.XpenselyServer.controller;
|
||||
package de.zendric.app.xpensely_server.controller;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpStatus;
|
||||
@@ -11,35 +11,39 @@ import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import de.zendric.app.XpenselyServer.model.AppUser;
|
||||
import de.zendric.app.XpenselyServer.services.UserService;
|
||||
import de.zendric.app.xpensely_server.model.AppUser;
|
||||
import de.zendric.app.xpensely_server.services.UserService;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/users")
|
||||
public class AppUserController {
|
||||
|
||||
|
||||
private UserService userService;
|
||||
|
||||
|
||||
@Autowired
|
||||
public AppUserController(UserService userService){
|
||||
this.userService= userService;
|
||||
public AppUserController(UserService userService) {
|
||||
this.userService = userService;
|
||||
}
|
||||
|
||||
|
||||
@GetMapping
|
||||
public AppUser getUser(@RequestParam Long id){
|
||||
public AppUser getUser(@RequestParam Long id) {
|
||||
return userService.getUser(id);
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
public ResponseEntity<AppUser> createUser(@RequestBody AppUser user){
|
||||
AppUser appUser = userService.createUser(user);
|
||||
return ResponseEntity.status(HttpStatus.CREATED).body(appUser);
|
||||
@GetMapping("/byName")
|
||||
public AppUser getUserByName(@RequestParam String username) {
|
||||
return userService.getUserByName(username);
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
public ResponseEntity<AppUser> createUser(@RequestBody AppUser user) {
|
||||
AppUser appUser = userService.createUser(user);
|
||||
return ResponseEntity.status(HttpStatus.CREATED).body(appUser);
|
||||
}
|
||||
|
||||
@DeleteMapping
|
||||
public String deleteUser(@RequestParam Long id){
|
||||
AppUser user = userService.deleteUserById(id);
|
||||
return "User deleted : "+ user.getUsername();
|
||||
public String deleteUser(@RequestParam Long id) {
|
||||
AppUser user = userService.deleteUserById(id);
|
||||
return "User deleted : " + user.getUsername();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,132 @@
|
||||
package de.zendric.app.xpensely_server.controller;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
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.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import de.zendric.app.xpensely_server.model.Expense;
|
||||
import de.zendric.app.xpensely_server.model.ExpenseList;
|
||||
import de.zendric.app.xpensely_server.services.ExpenseListService;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/expenselist")
|
||||
class ExpenseListController {
|
||||
|
||||
private ExpenseListService expenseListService;
|
||||
|
||||
@Autowired
|
||||
public ExpenseListController(ExpenseListService expenseListService) {
|
||||
this.expenseListService = expenseListService;
|
||||
}
|
||||
|
||||
@GetMapping("/all")
|
||||
public ResponseEntity<List<ExpenseList>> getAll() {
|
||||
try {
|
||||
List<ExpenseList> items = new ArrayList<>();
|
||||
|
||||
expenseListService.findAll().forEach(items::add);
|
||||
|
||||
if (items.isEmpty())
|
||||
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
|
||||
|
||||
return new ResponseEntity<>(items, HttpStatus.OK);
|
||||
} catch (Exception e) {
|
||||
return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
@GetMapping("/byUser")
|
||||
public ResponseEntity<List<ExpenseList>> getByUser(@RequestParam Long userId) {
|
||||
try {
|
||||
List<ExpenseList> items = expenseListService.findByUserId(userId);
|
||||
|
||||
if (items.isEmpty())
|
||||
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
|
||||
|
||||
return new ResponseEntity<>(items, HttpStatus.OK);
|
||||
} catch (Exception e) {
|
||||
return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
@GetMapping("/byUsername")
|
||||
public ResponseEntity<List<ExpenseList>> getByUser(@RequestParam String username) {
|
||||
try {
|
||||
List<ExpenseList> items = expenseListService.findByUsername(username);
|
||||
|
||||
if (items.isEmpty())
|
||||
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
|
||||
|
||||
return new ResponseEntity<>(items, HttpStatus.OK);
|
||||
} catch (Exception e) {
|
||||
return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
@GetMapping("/byId")
|
||||
public ResponseEntity<ExpenseList> getById(@RequestParam Long id) {
|
||||
Optional<ExpenseList> existingItemOptional = expenseListService.findById(id);
|
||||
|
||||
if (existingItemOptional.isPresent()) {
|
||||
return new ResponseEntity<>(existingItemOptional.get(), HttpStatus.OK);
|
||||
} else {
|
||||
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
|
||||
}
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
public ResponseEntity<ExpenseList> create(@RequestBody ExpenseList expenseList) {
|
||||
try {
|
||||
ExpenseList savedItem = (ExpenseList) expenseListService.save(expenseList);
|
||||
return new ResponseEntity<>(savedItem, HttpStatus.CREATED);
|
||||
} catch (Exception e) {
|
||||
return new ResponseEntity<>(null, HttpStatus.EXPECTATION_FAILED);
|
||||
}
|
||||
}
|
||||
|
||||
@DeleteMapping("{id}")
|
||||
public ResponseEntity<HttpStatus> delete(@PathVariable("id") Long id) {
|
||||
try {
|
||||
expenseListService.deleteById(id);
|
||||
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
|
||||
} catch (Exception e) {
|
||||
return new ResponseEntity<>(HttpStatus.EXPECTATION_FAILED);
|
||||
}
|
||||
}
|
||||
|
||||
@PostMapping("/{id}/add")
|
||||
public ResponseEntity<Expense> addExpenseToList(
|
||||
@PathVariable("id") Long expenseListId,
|
||||
@RequestBody Expense expense) {
|
||||
try {
|
||||
Expense addedExpense = expenseListService.addExpenseToList(expenseListId, expense);
|
||||
return new ResponseEntity<>(addedExpense, HttpStatus.CREATED);
|
||||
} catch (Exception e) {
|
||||
return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}/delete")
|
||||
public ResponseEntity<Expense> deleteExpenseFromList(
|
||||
@PathVariable("id") Long expenseListId,
|
||||
@RequestParam("expenseId") Long expenseId) {
|
||||
try {
|
||||
expenseListService.deleteExpenseFromList(expenseListId, expenseId);
|
||||
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
|
||||
} catch (Exception e) {
|
||||
return new ResponseEntity<>(null, HttpStatus.EXPECTATION_FAILED);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,26 +1,24 @@
|
||||
package de.zendric.app.XpenselyServer.model;
|
||||
package de.zendric.app.xpensely_server.model;
|
||||
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.GenerationType;
|
||||
import jakarta.persistence.Id;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
|
||||
@Entity
|
||||
@Getter
|
||||
@Setter
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@Entity
|
||||
public class AppUser {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
@Column(name = "username", nullable = false, unique = true)
|
||||
private String username;
|
||||
private String password;
|
||||
private String email;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package de.zendric.app.xpensely_server.model;
|
||||
|
||||
import java.time.LocalDate;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonBackReference;
|
||||
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.GenerationType;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.JoinColumn;
|
||||
import jakarta.persistence.ManyToOne;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@Entity
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class Expense {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
private String title;
|
||||
|
||||
@ManyToOne
|
||||
private AppUser owner;
|
||||
|
||||
private Double amount;
|
||||
private Double deviation;
|
||||
|
||||
private LocalDate date;
|
||||
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "expense_list_id", nullable = false)
|
||||
@JsonBackReference
|
||||
private ExpenseList expenseList;
|
||||
}
|
||||
@@ -1,7 +1,9 @@
|
||||
package de.zendric.app.XpenselyServer.model;
|
||||
package de.zendric.app.xpensely_server.model;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonManagedReference;
|
||||
|
||||
import jakarta.persistence.CascadeType;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
@@ -10,17 +12,21 @@ import jakarta.persistence.Id;
|
||||
import jakarta.persistence.ManyToMany;
|
||||
import jakarta.persistence.ManyToOne;
|
||||
import jakarta.persistence.OneToMany;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
|
||||
@Entity
|
||||
@Getter
|
||||
@Setter
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class ExpenseList {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
|
||||
private String name;
|
||||
|
||||
@ManyToOne
|
||||
@@ -29,6 +35,18 @@ public class ExpenseList {
|
||||
@ManyToMany
|
||||
private List<AppUser> sharedWith;
|
||||
|
||||
@OneToMany(mappedBy = "expenseList", cascade = CascadeType.ALL)
|
||||
@OneToMany(mappedBy = "expenseList", cascade = CascadeType.ALL, orphanRemoval = true)
|
||||
@JsonManagedReference
|
||||
private List<Expense> expenses;
|
||||
|
||||
public void addExpense(Expense expense) {
|
||||
expense.setExpenseList(this);
|
||||
expenses.add(expense);
|
||||
|
||||
}
|
||||
|
||||
public void removeExpense(Expense expense) {
|
||||
expenses.remove(expense);
|
||||
expense.setExpenseList(null);
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,13 @@
|
||||
package de.zendric.app.XpenselyServer.repo;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
import de.zendric.app.XpenselyServer.model.ExpenseList;
|
||||
package de.zendric.app.xpensely_server.repo;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import de.zendric.app.xpensely_server.model.ExpenseList;
|
||||
|
||||
@Repository
|
||||
public interface ExpenseListRepository extends JpaRepository<ExpenseList, Long> {
|
||||
List<ExpenseList> findByOwnerId(Long ownerId);
|
||||
}
|
||||
@@ -1,9 +1,10 @@
|
||||
package de.zendric.app.XpenselyServer.repo;
|
||||
package de.zendric.app.xpensely_server.repo;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import de.zendric.app.XpenselyServer.model.Expense;
|
||||
import de.zendric.app.xpensely_server.model.Expense;
|
||||
|
||||
@Repository
|
||||
public interface ExpenseRepository extends JpaRepository<Expense, Long> {}
|
||||
public interface ExpenseRepository extends JpaRepository<Expense, Long> {
|
||||
}
|
||||
@@ -1,9 +1,13 @@
|
||||
package de.zendric.app.XpenselyServer.repo;
|
||||
package de.zendric.app.xpensely_server.repo;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import de.zendric.app.XpenselyServer.model.AppUser;
|
||||
import de.zendric.app.xpensely_server.model.AppUser;
|
||||
|
||||
@Repository
|
||||
public interface UserRepository extends JpaRepository<AppUser, Long> {}
|
||||
public interface UserRepository extends JpaRepository<AppUser, Long> {
|
||||
Optional<AppUser> findByUsername(String username);
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package de.zendric.app.xpensely_server.security;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||
import org.springframework.security.web.SecurityFilterChain;
|
||||
|
||||
@Configuration
|
||||
public class SecurityConfig {
|
||||
|
||||
@Bean
|
||||
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
|
||||
http
|
||||
.authorizeHttpRequests(authorize -> authorize
|
||||
.anyRequest().permitAll() // Allow all requests without authentication
|
||||
)
|
||||
.csrf().disable(); // Disable CSRF for development purposes
|
||||
|
||||
return http.build();
|
||||
}
|
||||
|
||||
// @Bean
|
||||
// public SecurityFilterChain securityFilterChain(HttpSecurity http) throws
|
||||
// Exception {
|
||||
// return http.authorizeHttpRequests(auth -> {
|
||||
// auth.requestMatchers("/").permitAll();
|
||||
// auth.anyRequest().permitAll();
|
||||
// // auth.anyRequest().authenticated();
|
||||
// }).oauth2Login(Customizer.withDefaults())
|
||||
// .build();
|
||||
// }
|
||||
}
|
||||
@@ -0,0 +1,137 @@
|
||||
package de.zendric.app.xpensely_server.services;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import de.zendric.app.xpensely_server.model.AppUser;
|
||||
import de.zendric.app.xpensely_server.model.Expense;
|
||||
import de.zendric.app.xpensely_server.model.ExpenseList;
|
||||
import de.zendric.app.xpensely_server.repo.ExpenseListRepository;
|
||||
import de.zendric.app.xpensely_server.repo.ExpenseRepository;
|
||||
import jakarta.persistence.EntityManager;
|
||||
|
||||
@Service
|
||||
@Transactional
|
||||
public class ExpenseListService {
|
||||
|
||||
private ExpenseListRepository repository;
|
||||
private final ExpenseRepository expenseRepository;
|
||||
|
||||
@Autowired
|
||||
private EntityManager entityManager;
|
||||
|
||||
@Autowired
|
||||
public ExpenseListService(ExpenseListRepository repository, ExpenseRepository expenseRepository) {
|
||||
this.repository = repository;
|
||||
this.expenseRepository = expenseRepository;
|
||||
}
|
||||
|
||||
public List<ExpenseList> getAllLists() {
|
||||
return repository.findAll();
|
||||
}
|
||||
|
||||
public ExpenseList createList(ExpenseList list) {
|
||||
return repository.save(list);
|
||||
}
|
||||
|
||||
public void deleteList(Long id) {
|
||||
repository.deleteById(id);
|
||||
}
|
||||
|
||||
public void deleteById(Long id) {
|
||||
repository.deleteById(id);
|
||||
}
|
||||
|
||||
public Optional<ExpenseList> findById(Long id) {
|
||||
return repository.findById(id);
|
||||
}
|
||||
|
||||
public Iterable<ExpenseList> findAll() {
|
||||
return repository.findAll();
|
||||
}
|
||||
|
||||
public ExpenseList save(ExpenseList expenseList) {
|
||||
return repository.save(expenseList);
|
||||
}
|
||||
|
||||
public List<ExpenseList> findByUserId(Long id) {
|
||||
List<ExpenseList> allLists = repository.findAll();
|
||||
List<ExpenseList> userSpecificList = new ArrayList<>();
|
||||
for (ExpenseList expenseList : allLists) {
|
||||
List<AppUser> sharedWith = expenseList.getSharedWith();
|
||||
|
||||
if (expenseList.getOwner().getId().equals(id)) {
|
||||
userSpecificList.add(expenseList);
|
||||
} else {
|
||||
for (AppUser user : sharedWith) {
|
||||
if (user.getId().equals(id)) {
|
||||
userSpecificList.add(expenseList);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return userSpecificList;
|
||||
}
|
||||
|
||||
public List<ExpenseList> findByUsername(String username) {
|
||||
List<ExpenseList> allLists = repository.findAll();
|
||||
List<ExpenseList> userSpecificList = new ArrayList<>();
|
||||
for (ExpenseList expenseList : allLists) {
|
||||
List<AppUser> sharedWith = expenseList.getSharedWith();
|
||||
|
||||
if (expenseList.getOwner().getUsername().equals(username)) {
|
||||
userSpecificList.add(expenseList);
|
||||
} else {
|
||||
for (AppUser user : sharedWith) {
|
||||
if (user.getUsername().equals(username)) {
|
||||
userSpecificList.add(expenseList);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return userSpecificList;
|
||||
}
|
||||
|
||||
public Expense addExpenseToList(Long expenseListId, Expense expense) {
|
||||
ExpenseList expenseList = repository.findById(expenseListId)
|
||||
.orElseThrow(() -> new RuntimeException("ExpenseList not found with id: " + expenseListId));
|
||||
HashSet<Long> existingId = new HashSet<>();
|
||||
for (Expense e : expenseList.getExpenses()) {
|
||||
existingId.add(e.getId());
|
||||
}
|
||||
expenseList.addExpense(expense);
|
||||
repository.save(expenseList);
|
||||
Expense newExpense = expense;
|
||||
for (Expense e : expenseList.getExpenses()) {
|
||||
if (!existingId.contains(e.getId())) {
|
||||
newExpense = e;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return newExpense;
|
||||
}
|
||||
|
||||
public void deleteExpenseFromList(Long expenseListId, Long expenseId) {
|
||||
ExpenseList expenseList = repository.findById(expenseListId)
|
||||
.orElseThrow(() -> new RuntimeException("ExpenseList not found with id: " + expenseListId));
|
||||
Expense expenseToRemove = null;
|
||||
for (Expense expense : expenseList.getExpenses()) {
|
||||
if (expense.getId().equals(expenseId)) {
|
||||
expenseToRemove = expense;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (expenseToRemove != null) {
|
||||
expenseList.removeExpense(expenseToRemove);
|
||||
} else {
|
||||
throw new RuntimeException("Expense not found with id: " + expenseId);
|
||||
}
|
||||
repository.save(expenseList);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package de.zendric.app.xpensely_server.services;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import de.zendric.app.xpensely_server.repo.ExpenseRepository;
|
||||
|
||||
@Service
|
||||
@Transactional
|
||||
public class ExpenseService {
|
||||
|
||||
@Autowired
|
||||
private ExpenseRepository repository;
|
||||
|
||||
public ExpenseService(ExpenseRepository repository) {
|
||||
this.repository = repository;
|
||||
}
|
||||
}
|
||||
@@ -1,12 +1,12 @@
|
||||
package de.zendric.app.XpenselyServer.services;
|
||||
package de.zendric.app.xpensely_server.services;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import de.zendric.app.XpenselyServer.model.AppUser;
|
||||
import de.zendric.app.XpenselyServer.repo.UserRepository;
|
||||
import de.zendric.app.xpensely_server.model.AppUser;
|
||||
import de.zendric.app.xpensely_server.repo.UserRepository;
|
||||
|
||||
@Service
|
||||
public class UserService {
|
||||
@@ -25,19 +25,27 @@ public class UserService {
|
||||
}
|
||||
|
||||
public AppUser getUser(Long id) {
|
||||
Optional<AppUser> user= userRepository.findById(id);
|
||||
Optional<AppUser> user = userRepository.findById(id);
|
||||
if (user.isPresent()) {
|
||||
return user.get();
|
||||
}
|
||||
else return null;
|
||||
} else
|
||||
return null;
|
||||
}
|
||||
|
||||
public AppUser deleteUserById(Long id) {
|
||||
Optional<AppUser> user= userRepository.findById(id);
|
||||
Optional<AppUser> user = userRepository.findById(id);
|
||||
if (user.isPresent()) {
|
||||
userRepository.deleteById(id);
|
||||
return user.get();
|
||||
}
|
||||
else return null;
|
||||
} else
|
||||
return null;
|
||||
}
|
||||
|
||||
public AppUser getUserByName(String username) {
|
||||
Optional<AppUser> optUser = userRepository.findByUsername(username);
|
||||
if (optUser.isPresent()) {
|
||||
return optUser.get();
|
||||
} else
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -1 +1,16 @@
|
||||
#Server
|
||||
spring.application.name=XpenselyServer
|
||||
|
||||
#Security
|
||||
spring.security.enabled=false
|
||||
#logging.level.org.springframework.security=TRACE
|
||||
|
||||
# PostgreSQL Configuration
|
||||
spring.datasource.url=jdbc:postgresql://localhost:5432/Xpensely
|
||||
spring.datasource.username=${XpenselyDBUser}
|
||||
spring.datasource.password=${XpenselyDBPW}
|
||||
spring.datasource.driver-class-name=org.postgresql.Driver
|
||||
|
||||
# Hibernate configuration
|
||||
spring.jpa.hibernate.ddl-auto=update
|
||||
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package de.zendric.app.XpenselyServer;
|
||||
package de.zendric.app.xpensely_Server;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
Reference in New Issue
Block a user