This commit is contained in:
Jonas Weissengruber 2023-11-26 22:36:39 +01:00
parent 61bdf1e048
commit 74967d1cfa
8 changed files with 74 additions and 8 deletions

View file

@ -25,7 +25,10 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>

View file

@ -1,12 +1,16 @@
package me.jweissen.aeticket.controller;
import me.jweissen.aeticket.dto.response.CartEntryResponseDto;
import me.jweissen.aeticket.service.CartService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/cart")
public class CartController {
@ -16,8 +20,15 @@ public class CartController {
this.cartService = cartService;
}
/*@GetMapping("/list")
public ResponseEntity<List<CartEntryResponseDto>> getCartEntries() {
return cartService.get
}*/
@PostMapping("/add")
public ResponseEntity<Void> addEntry() {
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
}

View file

@ -1,4 +1,4 @@
package me.jweissen.aeticket.dto.response;
public record CartEntryResponseDto(Integer id, String name, Double price, Integer amount) {
public record CartEntryResponseDto(Long id, String name, Double price, Integer amount) {
}

View file

@ -0,0 +1,6 @@
package me.jweissen.aeticket.model;
public enum Role {
ADMIN,
USER,
}

View file

@ -4,14 +4,18 @@ import jakarta.persistence.*;
import lombok.Getter;
import lombok.NonNull;
import lombok.Setter;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import java.util.Collection;
import java.util.List;
@Entity
@Table
@Getter
@Setter
public class User {
public class User implements UserDetails {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@ -39,4 +43,38 @@ public class User {
@OneToMany(mappedBy = "user")
@JoinColumn(nullable = false)
private List<Cart> carts;
@Enumerated(EnumType.STRING)
private Role role;
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
return List.of(new SimpleGrantedAuthority(role.name()));
}
@Override
public String getUsername() {
return email;
}
@Override
public boolean isAccountNonExpired() {
return true;
}
@Override
public boolean isAccountNonLocked() {
return true;
}
@Override
public boolean isCredentialsNonExpired() {
return true;
}
@Override
public boolean isEnabled() {
return true;
}
}
}

View file

@ -14,7 +14,7 @@ public class CartEntryService {
return new CartEntryResponseDto(
category.getId(),
category.getName(),
category.getPrice(),
CategoryService.centsToEuros(category.getPrice()),
cartEntry.getAmount()
);
}

View file

@ -1,5 +1,6 @@
package me.jweissen.aeticket.service;
import me.jweissen.aeticket.dto.response.CartEntryResponseDto;
import me.jweissen.aeticket.dto.response.CartEventResponseDto;
import me.jweissen.aeticket.model.Cart;
import me.jweissen.aeticket.model.Event;
@ -33,4 +34,7 @@ public class CartService {
CartEntryService.toDtos(cartEntryRepository.getByCartAndEvent(cart.getId(), event.getId()))
)).toList();
}
//public List<CartEntryResponseDto> getCartByAuthToken() {
}

View file

@ -10,14 +10,18 @@ import java.util.List;
public class CategoryService {
public static CategoryResponseDto toDto(Category category) {
return new CategoryResponseDto(
category.getId(),
category.getName(),
category.getPrice() / 100.0,
category.getStock()
category.getId(),
category.getName(),
centsToEuros(category.getPrice()),
category.getStock()
);
}
public static List<CategoryResponseDto> toDtos(List<Category> categories) {
return categories.stream().map(CategoryService::toDto).toList();
}
public static Double centsToEuros(int cents) {
return cents / 100.0;
}
}