Chris Padilla/Blog

You can follow by RSS! (What's RSS?) Full posts list here.

    The Haps - May 2024

    It's summer time and I'm feeling inspired to spin my "now" page back up!

    Software

    I've been in software for 3 years now! Still enjoying my time at AptAmigo.

    So far, I've commited to learning a new programming language every year. This year, I've been having fun getting familiar with Java. The type system paired with VS Code intellisense is lovely.

    Now having had a few years of experience working professionally, I've really taken a liking to sharpening my thinking by writing short tech articles. It's a great space to share what I'm working on and find clear ways of explaining it. My favorite part is when someone comes across a problem I've faced already. I can reach back to my own post to help them through the solution!

    You can keep up with my tech articles through the tech tag on my blog. You can also find me on LinkedIn.

    Music

    šŸø
    A sample of what I'm writing ā€” Space Frog 64

    I'm bouncing between playing guitar, piano, and writing my own music. To paraphrase W.A. Mathieu, it's fun to have many pots boiling!

    After decades of communal music making and monophonic sound on the saxophone, I'm thrilled to be exploring independent and polyphonic instruments.

    To make things even more interesting, I'm putting most of my focus on learning jazz on piano and guitar. A friend of mine told me that it's actually the way to go ā€” to learn the vocabulary harmonically as opposed to in a scalar way.

    For music writing, it's been fun to keep it loose! I take what ever I'm listening to at the moment, deconstruct it, and turn it into something new. It's a wildly fun outlet!

    You can browse my mini albums here. You can keep up with my guitar and piano playing through the music tag on my blog. I'm also sharing recordings on Instagram.

    Art

    šŸ¶

    A couple of years ago I dedicated myself to learning how to draw and create digital paintings! I used to make my own comics growing up, but I always assumed my sister had all the talent. After switching from music to code, I realized I could learn anything. So I may as well learn a skill I've only ever dreamed of having! šŸ™‚

    I'm spending time balancing the fundamentals with just fooling around. This year has been all about figure drawing, direct painting, and making silly little digital sketches. The best part is that I have an excuse to revisit cartoons, comics, manga, and video games as sources of inspiration!

    You can see what I've made so far through the Art tag on my blog. I'm also sharing drawings on Instagram.

    Dallas

    Living in Dallas with Miranda and our dog Lucy! We moved from Austin a couple of years ago so Miranda could attend Parker University for their Doctor of Chiropractic program. Only a year left as of this writing, then we'll see where we end up next!

    So far it's been my favorite metro area in Texas to live in. Downtown is just a quick drive from where we are. But, in the other direction, artsy Denton is not far either.

    All good things!

    šŸ‘‹

    Billy Strayhorn ā€” Take the "A" Train

    Listen on Youtube

    šŸšž An oldie but a goodie

    Noodle Bird Cover Redux

    šŸ¦šŸœ

    My original album cover for Noodle Bird was pretty crunchy. šŸ˜… So I whipped up a new rendition!

    Two Years of Blogging

    From Austin Kleon:

    The way I show up for myself, the way I discover who I really am, is to make an appointment every day to show up to the page. If I show up to the page, I show up to myself.

    Two years of blogging! A small part of many years of creating and making, and it's certainly worth marking the occasion.

    Last year I shared my Lessons From a Year of Blogging. I'd largely echo those points this year. I still enjoy the clarity of thought that comes from writing and making, the ability to carry a long term conversation while leaving a trail, and having a stage to perform on.

    One other thing I've come to appreciate with blogging as a medium is this:

    It's Malleable

    I took a moment to look back at what I've made this year. For context: This started largely as a text blog, mostly developer logs. Out of the soil, buds of more literary posts on what I was reading began to sprout. Soon after that, I started sharing art and music.

    šŸŸs

    I've since traded words for color and sound! Lately I only write for my tech posts. And the switch is intentional ā€” I want to get more miles out of music and drawing, and there are only so many hours in the day.

    When I started, a was wringing my hands trying to decide what one thing I would blog about. What a relief that it doesn't have to be one niche!

    What I admire in the blogs I read is the spectrum of humanity that shines through. There's no predefined formula, and no algorithm to juice if you don't want to. So I end up seeing people as polyhedrons through the diversity they share and make.

    On top of that, tomorrow I could decide that I'm all in on fly fishing. There's no barrier to me making this blog all about that for a couple of years. And I would revel in being able to look back fondly on the different phases and eras.


    Starting out, I was worried about the idea that, if no one read what I wrote, I'd be doing all this for nothing. Turns out the rewards that come from a creative act alone absolutely eclipse any hit to the ego I would take if a post gets overlooked!

    Elizabeth Gilbert in Big Magic:

    You might end up dancing with royalty. Or you might just end up having to dance alone in the corner of the castle with your big, ungainly red foam claws waving in the empty air. that's fine, too. Sometimes it's like that. What you absolutely must not do is turn around and walk out. Otherwise, you will miss the party, and that would be a pity, because - please believe me - we did not come all this great distance, and make all this great effort, only to miss the party at the last moment.


    And, of course, even if my inbox doesn't get flooded with mail on the eureka moment someone had reading a tech blog post, writing publicly is still a phenomenal tool for clarifying thinking in that craft.


    I'm thinking much about the question offered through James Hollis' work: "What is seeking expression through me?" The wonderful thing about a personal blog is an opportunity to flexibly celebrate that expression.

    The generous part of that showing up is that, by doing so, we then bring our fullest selves to the people we serve.

    So start with where ever you are! Bring all parts of yourself along for the ride. You may find, as I have, it's an excellent way to explore the answers to that question.

    Layers in a Java Spring Boot API

    Spring Boot is a well embraced and powerful framework for developing web applications. Getting a backend API setup is fairly quick and allows for a great deal of durability up front. A fine balance between moving quickly on an app while giving you the guard rails to maintain organization, scalability, and durability.

    Here's the quick-start guide to getting an API setup:

    Layers

    If you're used to the MVC pattern in software development, you'll find a similar intent of organization around Spring Boot Architecture. However, the categories we group our logic in will be slightly different.

    In Spring Boot, these categories are referred to as layers of the application. They have their own directories and are bundled in their own package within the application.

    Here's a simple directory structure within the src folder of our app:

    project/src/main/java/com/projectname
    |-- controller
    |    |-- ArtistController.java
    |-- model
    |    |-- Artist.java
    |-- repositories
    |    |-- ArtistRepository.java
    +-- service
         |-- ArtistService.java

    Note that tests will live in their own directory outside of the src folder.

    Above, all layers are represented:

    • The Controller layer is responsible for connecting our data from the service layer with a means of rendering or presentation.
    • The Model layer handles communication between our application and external database.
    • The Repositories layer is responsible for querying logic to our database. If the model layer is establishing communication with the database, then the repositories layer is handling what type of requests we're making available to our application.
    • The Service layer handles all the business logic for our application. This layer relies on the repositories layer to provide the data so that it then can be transformed in whatever way we need it to be before delivery.

    In Action

    Ok! So say that I have my base app setup for a Spotify Popularity Monitor API. From here, I want to setup a feature for each layer that supports CRUD operations for an Artist.

    I'll start with the model:

    Model

    package com.chrispadilla.spotifyperformancemonitor.model;
    
    import jakarta.persistence.Column;
    import jakarta.persistence.Entity;
    import jakarta.persistence.GeneratedValue;
    import jakarta.persistence.GenerationType;
    import jakarta.persistence.Id;
    
    @Entity
    public class Artist {
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        private long id;
    
        @Column
        private String name;
    
        @Column
        private String[] genres;
    
        @Column
        private String spotifyId;
    
        @Column
        private String spotifyUrl;
    
        // Getters/Setters here
        
    }

    For brevity, I'm leaving out the Getters and Setters. What I'm showing is the schema for an Artist as I want it setup in my relational DB.

    By adding @Entity, I'm denoting this as a persistence layer class for the JPA.

    Next, I'll add the repository:

    Repository

    package com.chrispadilla.spotifyperformancemonitor.repositories;
    
    import org.springframework.data.repository.CrudRepository;
    import org.springframework.stereotype.Repository;
    
    import com.chrispadilla.spotifyperformancemonitor.model.Artist;
    
    @Repository
    public interface ArtistRepository extends CrudRepository<Artist, Long>{
        Artist findByName(String name);
    
        Artist findBySpotifyId(String spotifyId);
    }

    Fairly simple! Spring Repositories already come with several CRUD methods. I enable them by extending from CrudRepository. By default, I'll be able to call findById, but if I want to query by another field, I specify it on the interface with a method. I've done this with findByName and findBySpotifyId above. Spring will automatically make the connection that if I'm passing in a variable spotifyId, that this is what I'm querying by on the model. No extra logic required!

    Service

    The service layer will largely depend on what I'm trying to do with the data. It goes beyond the scope of today's blog, so here's the bare bones setup for a service class:

    package com.chrispadilla.spotifyperformancemonitor.service;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    import com.chrispadilla.spotifyperformancemonitor.model.Artist;
    
    
    
    @Service
    public class ArtistService implements IArtistService {
        @Autowired
        private ArtistRepository artistRepository;
            
    
        public void collectArtist(String spotifyId) {
            // Do your stuff
        }
    
    }

    It's good practice to create an interface outlining the methods you'll implement, so here I'm extending from an IArtistService instance above.

    Again, an annotation of @Service marks this as a bean that can be injected throughout my other layers.

    Controller

    It all comes together in the controller! Here I'll setup a controller to GET and POST artists:

    package com.chrispadilla.spotifyperformancemonitor.controller;
    
    import org.springframework.web.bind.annotation.RestController;
    import org.springframework.web.server.ResponseStatusException;
    
    import com.chrispadilla.spotifyperformancemonitor.model.Artist;
    import com.chrispadilla.spotifyperformancemonitor.repositories.ArtistRepository;
    import com.chrispadilla.spotifyperformancemonitor.service.ArtistService;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.http.HttpStatus;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseStatus;
    import org.springframework.web.bind.annotation.PostMapping;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestBody;
    
    
    @RestController
    @RequestMapping("/api/artist")
    public class ArtistController {
        @Autowired
        private ArtistRepository artistRepository;
        @Autowired
        private ArtistService artistService;
    
        @GetMapping
        @ResponseStatus(HttpStatus.OK)
        public Iterable<Artist> getArtists() {
            return artistRepository.findAll();
        }
    
        @GetMapping("/{id}")
        public Artist getArtist(@PathVariable Long id) {
            Artist artist = artistRepository.findById(id)
                .orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "No matching ID"));
            return artist;
        }
    
        @PostMapping
        @ResponseStatus(HttpStatus.CREATED)
        public Artist postArtist(@RequestBody Artist artist) {
            return artistRepository.save(artist);
        }
    }

    Several nice annotations are made available here! RequestMapping, GetMapping, and PostMapping allow us to define the path for our controller. PathVariable gives access to the id that's listed as part of my path in my get request.

    Here, you'll see I'm using Autowired to tell Spring to inject an instance of my repository and service layer classes created above. I can then use them freely within my controller.

    It can be tempting to write the business logic directly in the controller. To maintain flexibility, though, it's best to leave minimal db logic in the controller and wrap up data transformation within a method on your service layer classes. This allows for a more highly decoupled system. Were I to add a Presentation layer, it would be far simpler to access the service layer object, as opposed to refactoring a bloated controller method.

    That's It!

    With just a few files written, we have a strong basis for a flexible and easily extendable API! All with type checking along the way and a largely decoupled system! šŸ‘