Git Product home page Git Product logo

spring-undo's Introduction

Spring-Undo ↩️

Java CI with Gradle

Spring Boot starter that provides a way to easily implement undo functionality in your Spring Boot application.


Features

  • Drastically simplifies undo implementation. Reduces boilerplate that otherwise you need to manage.
  • Supports application scaling. Persist-module can use shared storage between all instances of your application. So you can call undo from any instance.
  • Spring-Undo is a Spring Boot Starter. You don't need to write any additional configuration for setting up Spring-Undo.

Usage

  1. Add the following dependencies in your build.gradle file:
implementation 'dev.fomenko:spring-undo-core:v0.0.1'
implementation 'dev.fomenko:spring-undo-redis:v0.0.1'

or pom.xml file:

<dependency>
    <groupId>dev.fomenko</groupId>
    <artifactId>spring-undo-core</artifactId>
    <version>0.0.1</version>
</dependency>
<dependency>
    <groupId>dev.fomenko</groupId>
    <artifactId>spring-undo-redis</artifactId>
    <version>0.0.1</version>
</dependency>
  1. Create a dto that will represent an event that can be undone.
@Data
@NoArgsConstructor
@AllArgsConstructor
public class UserRegisteredEvent {
    private String email;
}
  1. Register an event listener that will be called when either undo timeout is reached or undo is called.
@Component
public static class UserRegisteredUndoEventListener extends UndoEventListener<UserRegisteredEvent> {
    @Override
    public void onUndo(UserRegisteredEvent action) {
        // handle here undo logic
    }

    @Override
    public void onPersist(UserRegisteredEvent action) {
        // handle here what to do when event is persisted
        // For example, you can send an email to the user here
    }
}
  1. Autowire Undo bean and publish any object that represents an undoable action.
@RestController
@RequiredArgsConstructor
class UserController {
    private final Undo undo;

    @PostMapping("/register")
    public String register(RegistrationDto registration) {
        // main application logic
        var event = new UserRegisteredEvent("[email protected]");
        
        // publish method returns event id that can be used to undo the action
        return undo.publish(undoable);
    }
}
  1. Create a controller that will undo the action.
@RestController
public class UndoController {
    private final Undo undo;

    @GetMapping("/undo/{id}")
    public String undo(@PathVariable String id) {
        undo.undo(id);
        return "Undo successful";
    }
}
  1. You're done! 🎉

Contacts

Message me if you have any questions or suggestions: [email protected]

spring-undo's People

Contributors

michaelfmnk avatar

Stargazers

 avatar

Watchers

 avatar

spring-undo's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.