KEMBAR78
Crud Application | PDF | Software Engineering | Software
0% found this document useful (0 votes)
13 views6 pages

Crud Application

Uploaded by

Alex Abhisek
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views6 pages

Crud Application

Uploaded by

Alex Abhisek
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

CRUD APPLICATION

====================

User.java
---------

package com.abhi.Model;

import java.time.LocalDateTime;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.PrePersist;
import jakarta.persistence.PreUpdate;
import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.NotBlank;

@Entity
public class User {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@NotBlank(message = "Full name is mandatory")


@Column(nullable = false)
private String fullName;

@NotBlank(message = "Email is mandatory")


@Email(message = "Email should be valid")
@Column(nullable = false, unique = true)
private String email;

private String phone;

private String role;

@Column(nullable = false, updatable = false)


private LocalDateTime createdAt;

@Column(nullable = false)
private LocalDateTime updatedAt;

@NotBlank(message = "Password is mandatory")


@Column(nullable = false)
private String password;

@PrePersist
protected void onCreate() {
createdAt = LocalDateTime.now();
updatedAt = LocalDateTime.now();
}

@PreUpdate
protected void onUpdate() {
updatedAt = LocalDateTime.now();
}

// Default constructor
public User() {}

// Constructor with all fields except id, createdAt, updatedAt


public User(String fullName, String email, String phone, String role, String
password) {
this.fullName = fullName;
this.email = email;
this.phone = phone;
this.role = role;
this.password = password;
}

// Getters and Setters

public Long getId() {


return id;
}

public void setId(Long id) {


this.id = id;
}

public String getFullName() {


return fullName;
}

public void setFullName(String fullName) {


this.fullName = fullName;
}

public String getEmail() {


return email;
}

public void setEmail(String email) {


this.email = email;
}

public String getPhone() {


return phone;
}

public void setPhone(String phone) {


this.phone = phone;
}

public String getRole() {


return role;
}

public void setRole(String role) {


this.role = role;
}

public LocalDateTime getCreatedAt() {


return createdAt;
}

public void setCreatedAt(LocalDateTime createdAt) {


this.createdAt = createdAt;
}

public LocalDateTime getUpdatedAt() {


return updatedAt;
}

public void setUpdatedAt(LocalDateTime updatedAt) {


this.updatedAt = updatedAt;
}

public String getPassword() {


return password;
}

public void setPassword(String password) {


this.password = password;
}

UserController
==============

package com.abhi.controller;

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.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.server.ResponseStatusException;

import com.abhi.Model.User;
import com.abhi.repository.UserRepository;

import jakarta.validation.Valid;

@RestController
public class UserController {

@Autowired
private UserRepository userRepository;

@PostMapping("/api/users")
public User createUser(@Valid @RequestBody User user) {
return userRepository.save(user);
}

@GetMapping("/api/users")
public List<User> getUser() {
return userRepository.findAll();
}

@GetMapping("/api/users/{id}")
public User getUserById(@PathVariable Long id) {
return userRepository.findById(id)
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND,
"User not found with id " + id));
}

@PutMapping("/api/users/{id}")
public User updateUser(@Valid @RequestBody User user, @PathVariable Long id) {
User existingUser = userRepository.findById(id)
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND,
"User not found with id " + id));

existingUser.setFullName(user.getFullName());
existingUser.setEmail(user.getEmail());
existingUser.setRole(user.getRole());
existingUser.setPassword(user.getPassword());
existingUser.setPhone(user.getPhone()); // added phone update if needed

return userRepository.save(existingUser);
}

@DeleteMapping("/api/users/{id}")
public ResponseEntity<String> deleteUserById(@PathVariable Long id) {
User existingUser = userRepository.findById(id)
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND,
"User not found with id " + id));

userRepository.delete(existingUser);
return ResponseEntity.ok("User deleted successfully");
}
}

UserRepositiory
===============

package com.abhi.repository;

import org.springframework.data.jpa.repository.JpaRepository;

import com.abhi.Model.User;

public interface UserRepository extends JpaRepository<User, Long> {

Pom.xml
============
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
https://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.5.0</version>
<relativePath/> <!-- Lookup parent from repository -->
</parent>

<groupId>com.abhi</groupId>
<artifactId>user-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>user-service</name>
<description>User service for salon booking system</description>

<properties>
<java.version>17</java.version>
</properties>

<dependencies>

<!-- Spring Web -->


<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<!-- Spring Data JPA -->


<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<!-- MySQL Driver -->


<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>

<!-- Lombok -->


<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>

<!-- Spring Boot DevTools -->


<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>

<!-- Spring Boot Test -->


<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<!-- Swagger/OpenAPI -->


<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.5.0</version>
</dependency>

<!-- Validation -->


<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>

<!-- Optional: Spring Security -->


<!--
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
-->

</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>

You might also like