---
# System prepended metadata

title: Models
tags: [' spring-boot', parse]

---

---
tags: parse, spring-boot
---
# Models

## Serializable
```java=
package edu.famu.nytimebackend.models.serializable;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.lang.Nullable;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class SerializableFavorites {

    private @Nullable String favoriteId;
    private String bookId;
    private String title;
    private String description;
    private String author;
    private String image;
    private @Nullable String contributor;
    private @Nullable String amazon_link;
    private @Nullable String user;
}

```
## Parse

```java=
package edu.famu.nytimebackend.models;

import edu.famu.nytimebackend.models.serializable.SerializableFavorites;
import org.parse4j.*;

@ParseClassName("Favorites")
public class Favorites extends ParseObject {

    private final String BOOK_ID = "bookId";
    private final String TITLE = "title";
    private final String DESC = "description";
    private final String AUTHOR = "author";
    private final String IMG = "image";
    private final String CONTRIBUTOR = "contributor";
    private final String AMAZON = "amazon_link";
    private final String USER_ID = "userId";


    public void setBookId(String id)
    {
        put(BOOK_ID, id);
    }
    public String getBookId()
    {
        return getString(BOOK_ID);
    }

    public void setTitle(String title)
    {
        put(TITLE,title);
    }
    public String getTitle(){
        return getString(TITLE);
    }

    public void setDescription(String desc)
    {
        put(DESC, desc);
    }
    public String getDescription()
    {
        return getString(DESC);
    }

    public void setAuthor(String author)
    {
        put(AUTHOR, author);
    }
    public String getAuthor()
    {
        return getString(AUTHOR);
    }

    public void setImage(String image)
    {
        put(IMG, image);
    }
    public String getImage()
    {
        return getString(IMG);
    }

    public void setContributor(String contributor)
    {
        put(CONTRIBUTOR, contributor);
    }
    public String getContributor()
    {
        return getString(CONTRIBUTOR);
    }

    public void setAmazonLink(String link)
    {
        put(AMAZON, link);
    }
    public String getAmazonLink()
    {
        return getString(AMAZON);
    }

    public void setUser(String id)
    {
        ParseQuery<User> query = ParseQuery.getQuery(User.class);
        try {
            User user = query.get(id);
            put(USER_ID, user);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

    public SerializableFavorites getSerializable() {
        return new SerializableFavorites(getObjectId(), getBookId(), getTitle(), getDescription(), getAuthor(), getImage(), getContributor(), getAmazonLink(), null);
    }
}

```

# Services
```java=
package edu.famu.nytimebackend.services;

import edu.famu.nytimebackend.models.Favorites;
import edu.famu.nytimebackend.models.User;
import edu.famu.nytimebackend.models.serializable.SerializableFavorites;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.parse4j.ParseException;
import org.parse4j.ParseQuery;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class FavoritesService {

    protected final Log logger = LogFactory.getLog(this.getClass()); //used to write to the console

    public ArrayList<SerializableFavorites> getFavoriteByUser(String userId){
        ArrayList<SerializableFavorites> favorites = null;

        ParseQuery<User> userQuery = ParseQuery.getQuery(User.class);
        userQuery.whereEqualTo("objectId",userId);
        ParseQuery<Favorites> query = ParseQuery.getQuery(Favorites.class);
        query.whereMatchesQuery("userId",userQuery);

        try{
            List<Favorites> result = query.find();
            favorites = new ArrayList<>();
            for(Favorites f: result){
                favorites.add(f.getSerializable());
            }

        } catch (ParseException e) {
            logger.error("getFavoriteByUser Error: ", e);
        }

        return favorites;
    }

    public String addFavorite(SerializableFavorites favorite)  {
        String id = "Favorite is not added";
        Favorites fav = new Favorites();
        fav.setBookId(favorite.getBookId());
        fav.setTitle(favorite.getTitle());
        fav.setDescription(favorite.getDescription());
        fav.setAuthor(favorite.getAuthor());
        fav.setImage(favorite.getImage());
        fav.setContributor(favorite.getContributor());
        fav.setAmazonLink(favorite.getAmazon_link());
        fav.setUser(favorite.getUser());

        try {
            fav.save();
            id = fav.getObjectId();
        } catch(ParseException e) {
            e.printStackTrace();
        }
        return id;
    }

    public boolean removeFavorite(String id)
    {
        boolean status = false;
        ParseQuery<Favorites> query = ParseQuery.getQuery(Favorites.class);

        try {
            Favorites favorite = query.get(id);
            favorite.delete();
            status = true;
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return status;
    }
}
```

# Controller

```java=
package edu.famu.nytimebackend.controllers;

import edu.famu.nytimebackend.models.serializable.SerializableFavorites;
import edu.famu.nytimebackend.services.FavoritesService;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;


@RestController
@RequestMapping("/api/favorites")
public class FavoritesController {

    private FavoritesService favoritesService;

    public FavoritesController(FavoritesService favoritesService) {
        this.favoritesService = favoritesService;
    }

    @GetMapping("/{id}")
    public ArrayList<SerializableFavorites> getFavoritesByUser(@PathVariable(name = "id") String id){
        return favoritesService.getFavoriteByUser(id);
    }

    @PostMapping("/")
    public String addFavorite(@RequestBody SerializableFavorites favorite){
        return favoritesService.addFavorite(favorite);
    }

    @DeleteMapping("/{id}")
    public boolean removeFavorite(@PathVariable(name="id") String id){
        return favoritesService.removeFavorite(id);
    }
}
```