Giter Club home page Giter Club logo

birdwatching's Introduction

Birdwatching

A birdwatching RESTful API.

Technical details

  • Written in Java.
  • Uses Spring Framework.
  • Application runs on port 8080.
  • H2 database in memory mode.
    • Created once the application starts and dropped when the application shuts down.
    • Connection:
      • URL: jdbc:h2:tcp://localhost:9090/mem:birdwatching.
      • Username: sa.
      • Password: (empty).

Requirements

  • JDK 11 or superior.
  • Maven (optional).

Compile and run

Using maven wrapper:

$ ./mvnw clean spring-boot:run

Alternatively using installed maven version:

$ mvn clean spring-boot:run

Designing and implementation

Bird

@Entity
@Table(name = "bird")
public abstract class Bird extends BaseEntity {
    @Column(name = "name")
    private String name;

    @Enumerated
    private BirdSize size;

    @Column(name = "photo_url")
    private String photoURL;

    @ElementCollection(targetClass = Color.class, fetch = FetchType.EAGER)
    @CollectionTable(name = "bird_color", joinColumns = @JoinColumn(name = "bird_id"))
    @Column(name = "color")
    @Enumerated
    private List<Color> colors = new ArrayList<>();

    // Constructors and methods removed for brevity
}
  • BirdSize is an enum.
  • Color is an enum. Plumage colors are represented as a list of them. The restriction number (four) should by implemented by code validation.

NaturalReserve

@Entity
@Table(name = "natural_reserve")
public class NaturalReserve extends BaseEntity {

    @Column(name = "name")
    private String name;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "region_id")
    private Region region;

    // Just for cascade delete, no getter and setter
    @OneToMany(mappedBy = "reserve", cascade = CascadeType.REMOVE, orphanRemoval = true,
               fetch = FetchType.LAZY)
    private List<Chance> chances = new ArrayList<>();

    // Constructors and methods removed for brevity
}
  • Region is an entity itself with id and name, so in order to create or update a NaturalReserve entity a Region#id must be provided.

Chance

@Entity
@Table(name = "chance")
public class Chance extends BaseEntity {

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "bird_id")
    private Bird bird;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "natural_reserve_id")
    private NaturalReserve reserve;

    @Enumerated
    private Month month;

    @Column(name = "probability")
    private Double probability;

    // Constructors and methods removed for brevity
}
  • Chance is an entity which represents the probability of seeing a bird in a natural reserve depending on the month.

Region

@Entity
@Table(name = "region")
public class Region extends BaseEntity {

    @Column(name = "name")
    private String name;

    // Constructors and methods removed for brevity
}

Services

Examples can be found in doc/examples.sh.

Birds

Method URL Description
GET /api/v1/birds Retrieve all birds

Natural reserves

Method URL Description
GET /api/v1/reserves Retrieve all natural reserves
GET /api/v1/reserves/{id} Retrieve a natural reserves
POST /api/v1/reserves Create a natural reserve
PUT /api/v1/reserves/{id} Update a natural reserve
DELETE /api/v1/reserves/{id} Delete a natural reserve

Chances

Method URL Description
GET /api/v1/chances?date={date} Retrieve all chances by month

Regions

Method URL Description
GET /api/v1/regions Retrieve all regions

Database

After the database is created, two scripts (a DDL and a DML) are executed. These files are:

  • src/main/resources/schema.sql
  • src/main/resources/data.sql

Copyright

Copyright © 2020 Lisandro Fernandez. See LICENSE for details.

birdwatching's People

Contributors

lisandrofernandez avatar

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.