GreyWolf18
GreyWolf18

Reputation: 141

Rest API order by name

I'm trying to create my own REST API and I'm having trouble trying to order my data by name. currently, I am able to display all the data from the styles table, however, I wish to sort them alphabetically.

I was able to do a filter by extracting the year from the date and checking if that was in the database, this is shown in

   public List<Beers> getAllBeersByYear(int year) {
        EntityManager em = DBUtil.getEMF().createEntityManager();

        List<Beers> list = null;
        List<Beers> beersToRemove = new ArrayList<>();
        try {
            list = em.createNamedQuery("Beers.findAll", Beers.class)
                    .getResultList();
            if (list == null || list.isEmpty()) {
                list = null;
            }

        } finally {
            em.close();
        }
        Calendar cal = Calendar.getInstance();
        for (Beers beer : list) {
            cal.setTime(beer.getLastMod());
            if (cal.get(Calendar.YEAR) != year) {

                beersToRemove.add(beer);
            }
        }
        list.removeAll(beersToRemove);
        return list;

    }

the controller is

  @GetMapping(produces=MediaType.APPLICATION_JSON_VALUE)
    public List<Styles> GetAllStyles() {

        return service.getAllStyles();
    }

would it be possible to do something similar to the service and controller where instead of filtering the data, it can sort by the name of a column

the service is

  public List<Styles> getAllStyles() {
        EntityManager em = DBUtil.getEMF().createEntityManager();

        List<Styles> list = null;

        try {
            list = em.createNamedQuery("Styles.findAll", Styles.class)
                    .getResultList();
            if (list == null || list.isEmpty()) {
                list = null;
            }

        } finally {
            em.close();
        }
        return list;

    }

the JPA I am using is

@Entity
@Table(name = "styles")
@NamedQueries({
    @NamedQuery(name = "Styles.findAll", query = "SELECT s FROM Styles s"),
    @NamedQuery(name = "Styles.findById", query = "SELECT s FROM Styles s WHERE s.id = :id"),
    @NamedQuery(name = "Styles.findByCatId", query = "SELECT s FROM Styles s WHERE s.catId = :catId"),
    @NamedQuery(name = "Styles.findByStyleName", query = "SELECT s FROM Styles s WHERE s.styleName = :styleName"),
    @NamedQuery(name = "Styles.findByLastMod", query = "SELECT s FROM Styles s WHERE s.lastMod = :lastMod")})
public class Styles implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id")
    private Integer id;
    @Basic(optional = false)
    @NotNull
    @Column(name = "cat_id")
    private int catId;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 255)
    @Column(name = "style_name")
    private String styleName;
    @Basic(optional = false)
    @NotNull
    @Column(name = "last_mod")
    @Temporal(TemporalType.TIMESTAMP)
    private Date lastMod;

    public Styles() {
    }

    public Styles(Integer id) {
        this.id = id;
    }

    public Styles(Integer id, int catId, String styleName, Date lastMod) {
        this.id = id;
        this.catId = catId;
        this.styleName = styleName;
        this.lastMod = lastMod;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public int getCatId() {
        return catId;
    }

    public void setCatId(int catId) {
        this.catId = catId;
    }

    public String getStyleName() {
        return styleName;
    }

    public void setStyleName(String styleName) {
        this.styleName = styleName;
    }

    public Date getLastMod() {
        return lastMod;
    }

    public void setLastMod(Date lastMod) {
        this.lastMod = lastMod;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Styles)) {
            return false;
        }
        Styles other = (Styles) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "Service.Styles[ id=" + id + " ]";
    }

}

Upvotes: 0

Views: 1260

Answers (2)

Simon Martinelli
Simon Martinelli

Reputation: 36143

Just create another query:

@NamedQuery(name = "Styles.findAll", query = "SELECT s FROM Styles s ORDER BY s.name")

And why are you filtering in the code when you can add a query with a where condition?

Upvotes: 0

Related Questions