danielrincon-m / IETI_LAB1_PART1

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Spring Boot Rest API

Learning Objectives

  • Explain what needs to be donde to achieve the Level 2 on a RESTFUL API on the Richardson Maturity Model.
  • Implement a Level 2 Users RESTFUL API Microservice.
  • Implement a Level 2 Tasks RESTFUL API Microservice.
  • User dependencies injections to create a decoupled architecture.

Growth Mindset 🤹🏽

"Individuals who believe their talents can be developed (through hard work, good strategies, and input from others) have a growth mindset. They tend to achieve more than those with a more fixed mindset (those who believe their talents are innate gifts)" What Having a "Growth Mindset" Actually means - Harvard Business Review

Main Topics

  • Microservices.
  • RESTFUL API.
  • Richardson Maturity Model.
  • Dependencies Injection.

Codelab 🧪

🗣️ "I hear and I forget I see and I remember I do and I understand." Confucius

Part 1: Implementing the Users Microservice RESTFUL API

  1. Create a new project using the Spring Initializr
  • Use either Java or Kotlin as programming language.
  • Use Gradle as project option(if your computer is slow then use Maven)
  • Add Spring Web dependency before generating the project.
  1. Create a new repository on Github and commit the files generated in 1.
  2. Create a new package called dto and inside define your UserDto object with at least the following fields:
    • name.
    • email.
    • lastName.
    • createdAt.
  3. Create a new package called data and inside define your User data object with at least the following fields:
    • id.
    • name.
    • email.
    • lastName.
    • createdAt.
  4. Create a new package called service an inside create the following interface:

Java:

    public interface UserService
    {
        User create( User user );

        User findById( String id );
        
        List<User> all();

        void deleteById( String id );

        User update( User user, String userId );
    }

Kotlin:

    interface UserService {

       fun create( user: User): User

       fun findById( String: id ): User?
       
       fun  all(): List<User>

       fun deleteById( String: id )

       fun update( User: user, String: userId ): User

    }
  1. Create an implementation of the UserService using a HashMap data structure inside.
  2. Make your service implementation UserServiceHashMap injectable using the @Service annotation.
  3. Create a new package called controller and create a new class UserController inside.
  4. Annotate your UserController so it becomes a REST Controller:

Java:

  @RestController
  @RequestMapping( "/v1/user" )
  public class UserController
  {
  }

Kotlin:

 @RestController
@RequestMapping("/v1/user")
class UserController
  1. Inject your UserService implementation inside the UserController via the constructor:

Java:

  @RestController
  @RequestMapping( "/v1/user" )
  public class UserController
  {
      private final UserService userService;

      public UserController(@Autowired UserService userService )
      {
          this.userService = userService;
      }   
  }

Kotlin:

 @RestController
 @RequestMapping( "/v1/user" )
 class UserController(@Autowired private val userService: UserService)
  1. Implement all the endpoints needed to interact with you UserService. Use the following method signatures to help you achieve the Level 2 RESTFUL Maturity:

Java:

  @RestController
  @RequestMapping( "/v1/user" )
  public class UserController
  {
     private final UserService userService;

     public UserController( UserService userService )
     {
         this.userService = userService;
     }

  
     @GetMapping
     public ResponseEntity<List<User>> all()
     {
         //TODO implement this method using UserService
         return null;
     }
     
     @GetMapping( "/{id}" )
     public ResponseEntity<User> findById( @PathVariable String id )
     {
        //TODO implement this method using UserService
        return null;
     }
     
     
     @PostMapping
     public ResponseEntity<User> create( @RequestBody UserDto userDto )
     {
          //TODO implement this method using UserService
         return null;
     }
     
     @PutMapping( "/{id}" )
     public ResponseEntity<User> update( @RequestBody UserDto userDto, @PathVariable String id )
     {
          //TODO implement this method using UserService
         return null;
     }

     @DeleteMapping( "/{id}" )
     public ResponseEntity<Boolean> delete( @PathVariable String id )
     {
          //TODO implement this method using UserService
         return null;      
     }
  }      

Kotlin:

 @RestController
 @RequestMapping( "/v1/user" )
 class UserController(@Autowired private val userService: UserService)
 {
    @GetMapping
    fun all(): ResponseEntity<List<User>>
    {
        //TODO implement this method using UserService
        return null
    }
    
    @GetMapping( "/{id}" )
    fun findById( @PathVariable id: String ): ResponseEntity<User> 
    {
       //TODO implement this method using UserService
       return null
    }
    
    
    @PostMapping
    fun create( @RequestBody  userDto: UserDto): ResponseEntity<User>
    {
         //TODO implement this method using UserService
        return null
    }
    
    @PutMapping( "/{id}" )
    fun update( @RequestBody userDto: UserDto, @PathVariable id: String): ResponseEntity<User> 
    {
         //TODO implement this method using UserService
        return null
    }

    @DeleteMapping( "/{id}" )
    fun delete( @PathVariable id: String): ResponseEntity<Boolean>
    {
         //TODO implement this method using UserService
        return null     
    }   
 
 
 }
  1. Download and install Postman and test ALL the endpoints of your API.

About


Languages

Language:Java 100.0%