initial Release request #1
@@ -19,6 +19,7 @@ import org.springframework.web.bind.annotation.RestController;
|
|||||||
|
|
||||||
import de.zendric.app.xpensely_server.model.AppUser;
|
import de.zendric.app.xpensely_server.model.AppUser;
|
||||||
import de.zendric.app.xpensely_server.model.Expense;
|
import de.zendric.app.xpensely_server.model.Expense;
|
||||||
|
import de.zendric.app.xpensely_server.model.ExpenseInput;
|
||||||
import de.zendric.app.xpensely_server.model.ExpenseList;
|
import de.zendric.app.xpensely_server.model.ExpenseList;
|
||||||
import de.zendric.app.xpensely_server.model.InviteRequest;
|
import de.zendric.app.xpensely_server.model.InviteRequest;
|
||||||
import de.zendric.app.xpensely_server.services.ExpenseListService;
|
import de.zendric.app.xpensely_server.services.ExpenseListService;
|
||||||
@@ -95,9 +96,23 @@ class ExpenseListController {
|
|||||||
@PostMapping
|
@PostMapping
|
||||||
public ResponseEntity<ExpenseList> create(@RequestBody ExpenseList expenseList) {
|
public ResponseEntity<ExpenseList> create(@RequestBody ExpenseList expenseList) {
|
||||||
try {
|
try {
|
||||||
ExpenseList savedItem = (ExpenseList) expenseListService.save(expenseList);
|
// Fetch or persist the `owner` AppUser
|
||||||
|
if (expenseList.getOwner() != null) {
|
||||||
|
AppUser existingOwner = userService.getUser(expenseList.getOwner().getId());
|
||||||
|
if (existingOwner == null) {
|
||||||
|
throw new IllegalArgumentException("Owner does not exist.");
|
||||||
|
}
|
||||||
|
expenseList.setOwner(existingOwner);
|
||||||
|
} else {
|
||||||
|
throw new IllegalArgumentException("Owner is required.");
|
||||||
|
}
|
||||||
|
|
||||||
|
expenseList.setSharedWith(null);
|
||||||
|
|
||||||
|
ExpenseList savedItem = expenseListService.createList(expenseList);
|
||||||
return new ResponseEntity<>(savedItem, HttpStatus.CREATED);
|
return new ResponseEntity<>(savedItem, HttpStatus.CREATED);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
return new ResponseEntity<>(null, HttpStatus.EXPECTATION_FAILED);
|
return new ResponseEntity<>(null, HttpStatus.EXPECTATION_FAILED);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -115,8 +130,11 @@ class ExpenseListController {
|
|||||||
@PostMapping("/{id}/add")
|
@PostMapping("/{id}/add")
|
||||||
public ResponseEntity<Expense> addExpenseToList(
|
public ResponseEntity<Expense> addExpenseToList(
|
||||||
@PathVariable("id") Long expenseListId,
|
@PathVariable("id") Long expenseListId,
|
||||||
@RequestBody Expense expense) {
|
@RequestBody ExpenseInput expenseInput) {
|
||||||
try {
|
try {
|
||||||
|
AppUser expenseOwner = userService.getUserByName(expenseInput.getOwner());
|
||||||
|
Expense expense = expenseInput.convertToExpense(expenseOwner.getId());
|
||||||
|
|
||||||
Expense addedExpense = expenseListService.addExpenseToList(expenseListId, expense);
|
Expense addedExpense = expenseListService.addExpenseToList(expenseListId, expense);
|
||||||
return new ResponseEntity<>(addedExpense, HttpStatus.CREATED);
|
return new ResponseEntity<>(addedExpense, HttpStatus.CREATED);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
@@ -153,6 +171,9 @@ class ExpenseListController {
|
|||||||
if (list.getSharedWith() != null) {
|
if (list.getSharedWith() != null) {
|
||||||
return ResponseEntity.status(HttpStatus.IM_USED).body("List has already been shared");
|
return ResponseEntity.status(HttpStatus.IM_USED).body("List has already been shared");
|
||||||
}
|
}
|
||||||
|
if (list.getOwner().getId() == inviteRequest.getUserId()) {
|
||||||
|
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("You cant join your own List");
|
||||||
|
}
|
||||||
AppUser user = null;
|
AppUser user = null;
|
||||||
try {
|
try {
|
||||||
user = userService.getUser(inviteRequest.getUserId());
|
user = userService.getUser(inviteRequest.getUserId());
|
||||||
|
|||||||
@@ -0,0 +1,50 @@
|
|||||||
|
package de.zendric.app.xpensely_server.model;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
|
|
||||||
|
import jakarta.persistence.GeneratedValue;
|
||||||
|
import jakarta.persistence.GenerationType;
|
||||||
|
import jakarta.persistence.Id;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
import lombok.Setter;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
public class ExpenseInput {
|
||||||
|
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
|
private Long id;
|
||||||
|
|
||||||
|
private String title;
|
||||||
|
|
||||||
|
private String owner;
|
||||||
|
|
||||||
|
private Double amount;
|
||||||
|
private Double deviation;
|
||||||
|
|
||||||
|
private LocalDate date;
|
||||||
|
|
||||||
|
private ExpenseList expenseList;
|
||||||
|
|
||||||
|
public Expense convertToExpense(Long userId) {
|
||||||
|
AppUser appUser = new AppUser();
|
||||||
|
appUser.setId(userId);
|
||||||
|
appUser.setUsername(owner);
|
||||||
|
|
||||||
|
Expense expense = new Expense();
|
||||||
|
expense.setAmount(amount);
|
||||||
|
expense.setDate(date);
|
||||||
|
expense.setDeviation(deviation);
|
||||||
|
expense.setExpenseList(expenseList);
|
||||||
|
expense.setId(id);
|
||||||
|
expense.setOwner(appUser);
|
||||||
|
expense.setTitle(title);
|
||||||
|
|
||||||
|
return expense;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -98,15 +98,20 @@ public class ExpenseListService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public Expense addExpenseToList(Long expenseListId, Expense expense) {
|
public Expense addExpenseToList(Long expenseListId, Expense expense) {
|
||||||
|
// find expenseList
|
||||||
ExpenseList expenseList = repository.findById(expenseListId)
|
ExpenseList expenseList = repository.findById(expenseListId)
|
||||||
.orElseThrow(() -> new RuntimeException("ExpenseList not found with id: " + expenseListId));
|
.orElseThrow(() -> new RuntimeException("ExpenseList not found with id: " + expenseListId));
|
||||||
|
// get all added expenses
|
||||||
HashSet<Long> existingId = new HashSet<>();
|
HashSet<Long> existingId = new HashSet<>();
|
||||||
for (Expense e : expenseList.getExpenses()) {
|
for (Expense e : expenseList.getExpenses()) {
|
||||||
existingId.add(e.getId());
|
existingId.add(e.getId());
|
||||||
}
|
}
|
||||||
|
// add the new expense
|
||||||
expenseList.addExpense(expense);
|
expenseList.addExpense(expense);
|
||||||
|
// save
|
||||||
repository.save(expenseList);
|
repository.save(expenseList);
|
||||||
Expense newExpense = expense;
|
|
||||||
|
Expense newExpense = new Expense();
|
||||||
for (Expense e : expenseList.getExpenses()) {
|
for (Expense e : expenseList.getExpenses()) {
|
||||||
if (!existingId.contains(e.getId())) {
|
if (!existingId.contains(e.getId())) {
|
||||||
newExpense = e;
|
newExpense = e;
|
||||||
@@ -137,14 +142,18 @@ public class ExpenseListService {
|
|||||||
public String generateInviteCode(Long listId) {
|
public String generateInviteCode(Long listId) {
|
||||||
ExpenseList list = repository.findById(listId)
|
ExpenseList list = repository.findById(listId)
|
||||||
.orElseThrow(() -> new RuntimeException("List not found"));
|
.orElseThrow(() -> new RuntimeException("List not found"));
|
||||||
|
String inviteCode;
|
||||||
|
if (list.getInviteCode() == null || list.getInviteCodeExpiration().isBefore(LocalDateTime.now())) {
|
||||||
|
|
||||||
String inviteCode = UUID.randomUUID().toString().substring(0, 6).toUpperCase();
|
inviteCode = UUID.randomUUID().toString().substring(0, 6).toUpperCase();
|
||||||
LocalDateTime expirationTime = LocalDateTime.now().plusWeeks(1);
|
LocalDateTime expirationTime = LocalDateTime.now().plusWeeks(1);
|
||||||
|
|
||||||
list.setInviteCode(inviteCode);
|
|
||||||
list.setInviteCodeExpiration(expirationTime);
|
|
||||||
repository.save(list);
|
|
||||||
|
|
||||||
|
list.setInviteCode(inviteCode);
|
||||||
|
list.setInviteCodeExpiration(expirationTime);
|
||||||
|
repository.save(list);
|
||||||
|
} else {
|
||||||
|
inviteCode = list.getInviteCode();
|
||||||
|
}
|
||||||
return inviteCode;
|
return inviteCode;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user