Search in sources :

Example 6 with Sakura

use of mingzuozhibi.persist.disc.Sakura in project mzzb-server by mingzuozhibi.

the class SakuraController method addOne.

@Transactional
@PreAuthorize("hasRole('BASIC')")
@PostMapping(value = "/api/sakuras", produces = MEDIA_TYPE)
public String addOne(@JsonArg String key, @JsonArg String title, @JsonArg(defaults = "true") boolean enabled, @JsonArg(defaults = "PublicList") ViewType viewType) {
    if (key.isEmpty()) {
        if (LOGGER.isWarnEnabled()) {
            warnRequest("[创建列表失败][列表索引不能为空]");
        }
        return errorMessage("列表索引不能为空");
    }
    if (title.isEmpty()) {
        if (LOGGER.isWarnEnabled()) {
            warnRequest("[创建列表失败][列表标题不能为空]");
        }
        return errorMessage("列表标题不能为空");
    }
    if (dao.lookup(Sakura.class, "key", key) != null) {
        if (LOGGER.isInfoEnabled()) {
            infoRequest("[创建列表失败][该列表索引已存在][Key={}]", key);
        }
        return errorMessage("该列表索引已存在");
    }
    Sakura sakura = new Sakura(key, title, enabled, viewType);
    dao.save(sakura);
    JSONObject result = sakura.toJSON();
    if (LOGGER.isInfoEnabled()) {
        infoRequest("[创建列表成功][列表信息={}]", result);
    }
    return objectResult(result);
}
Also used : JSONObject(org.json.JSONObject) Sakura(mingzuozhibi.persist.disc.Sakura) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) Transactional(org.springframework.transaction.annotation.Transactional)

Example 7 with Sakura

use of mingzuozhibi.persist.disc.Sakura in project mzzb-server by mingzuozhibi.

the class SakuraController method findDiscs.

@Transactional
@GetMapping(value = "/api/sakuras/key/{key}/discs", produces = MEDIA_TYPE)
public String findDiscs(@PathVariable String key, @RequestParam(defaultValue = DISC_COLUMNS) String discColumns, @RequestParam(name = "public", defaultValue = "true") boolean isPublic) {
    Sakura sakura = dao.lookup(Sakura.class, "key", key);
    if (sakura == null) {
        if (LOGGER.isWarnEnabled()) {
            warnRequest("[获取列表碟片失败][指定的列表索引不存在][key={}]", key);
        }
        return errorMessage("指定的列表索引不存在");
    }
    JSONObject result = sakura.toJSON();
    JSONArray discs = new JSONArray();
    sakura.getDiscs().stream().filter(disc -> !isPublic || disc.getUpdateType() != UpdateType.None).forEach(disc -> discs.put(disc.toJSON(getColumns(discColumns))));
    result.put("discs", discs);
    if (LOGGER.isDebugEnabled()) {
        debugRequest("[获取列表碟片成功][列表标题={}][碟片数量={}]", sakura.getTitle(), sakura.getDiscs().size());
    }
    return objectResult(result);
}
Also used : Criteria(org.hibernate.Criteria) Restrictions(org.hibernate.criterion.Restrictions) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) Set(java.util.Set) Disc(mingzuozhibi.persist.disc.Disc) List(java.util.List) JSONObject(org.json.JSONObject) ViewType(mingzuozhibi.persist.disc.Sakura.ViewType) UpdateType(mingzuozhibi.persist.disc.Disc.UpdateType) Sakura(mingzuozhibi.persist.disc.Sakura) org.springframework.web.bind.annotation(org.springframework.web.bind.annotation) JsonArg(mingzuozhibi.support.JsonArg) DiscController.buildSet(mingzuozhibi.action.DiscController.buildSet) JSONArray(org.json.JSONArray) Transactional(org.springframework.transaction.annotation.Transactional) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) Sakura(mingzuozhibi.persist.disc.Sakura) Transactional(org.springframework.transaction.annotation.Transactional)

Example 8 with Sakura

use of mingzuozhibi.persist.disc.Sakura in project mzzb-server by mingzuozhibi.

the class SakuraController method setOne.

@Transactional
@PreAuthorize("hasRole('BASIC')")
@PutMapping(value = "/api/sakuras/{id}", produces = MEDIA_TYPE)
public String setOne(@PathVariable("id") Long id, @JsonArg("$.key") String key, @JsonArg("$.title") String title, @JsonArg("$.viewType") ViewType viewType, @JsonArg("$.enabled") boolean enabled) {
    if (key.isEmpty()) {
        if (LOGGER.isWarnEnabled()) {
            warnRequest("[编辑列表失败][列表索引不能为空]");
        }
        return errorMessage("列表索引不能为空");
    }
    if (title.isEmpty()) {
        if (LOGGER.isWarnEnabled()) {
            warnRequest("[编辑列表失败][列表标题不能为空]");
        }
        return errorMessage("列表标题不能为空");
    }
    Sakura sakura = dao.get(Sakura.class, id);
    if (sakura == null) {
        if (LOGGER.isWarnEnabled()) {
            warnRequest("[编辑列表失败][指定的列表Id不存在][id={}]", id);
        }
        return errorMessage("指定的列表Id不存在");
    }
    JSONObject before = sakura.toJSON();
    if (LOGGER.isDebugEnabled()) {
        debugRequest("[编辑列表开始][修改前={}]", before);
    }
    sakura.setKey(key);
    sakura.setTitle(title);
    sakura.setViewType(viewType);
    sakura.setEnabled(enabled);
    JSONObject result = sakura.toJSON();
    if (LOGGER.isDebugEnabled()) {
        debugRequest("[编辑列表成功][修改后={}]", result);
    }
    return objectResult(result);
}
Also used : JSONObject(org.json.JSONObject) Sakura(mingzuozhibi.persist.disc.Sakura) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) Transactional(org.springframework.transaction.annotation.Transactional)

Example 9 with Sakura

use of mingzuozhibi.persist.disc.Sakura in project mzzb-server by mingzuozhibi.

the class SakuraController method findOne.

@Transactional
@GetMapping(value = "/api/sakuras/key/{key}", produces = MEDIA_TYPE)
public String findOne(@PathVariable String key) {
    Sakura sakura = dao.lookup(Sakura.class, "key", key);
    if (sakura == null) {
        if (LOGGER.isWarnEnabled()) {
            warnRequest("[获取列表失败][指定的列表索引不存在][key={}]", key);
        }
        return errorMessage("指定的列表索引不存在");
    }
    JSONObject result = sakura.toJSON();
    if (LOGGER.isDebugEnabled()) {
        debugRequest("[获取列表成功][列表信息={}]", sakura.toJSON());
    }
    return objectResult(result);
}
Also used : JSONObject(org.json.JSONObject) Sakura(mingzuozhibi.persist.disc.Sakura) Transactional(org.springframework.transaction.annotation.Transactional)

Example 10 with Sakura

use of mingzuozhibi.persist.disc.Sakura in project mzzb-server by mingzuozhibi.

the class SakuraController method pushDiscs.

@Transactional
@PreAuthorize("hasRole('BASIC')")
@PostMapping(value = "/api/sakuras/{id}/discs/{discId}", produces = MEDIA_TYPE)
public String pushDiscs(@PathVariable("id") Long id, @PathVariable("discId") Long discId, @RequestParam(name = "discColumns", defaultValue = DISC_COLUMNS) String discColumns) {
    Sakura sakura = dao.get(Sakura.class, id);
    if (sakura == null) {
        if (LOGGER.isWarnEnabled()) {
            warnRequest("[添加碟片到列表失败][指定的列表Id不存在][Id={}]", id);
        }
        return errorMessage("指定的列表Id不存在");
    }
    Disc disc = dao.get(Disc.class, discId);
    if (disc == null) {
        if (LOGGER.isWarnEnabled()) {
            warnRequest("[添加碟片到列表失败][指定的碟片Id不存在][Id={}]", discId);
        }
        return errorMessage("指定的碟片Id不存在");
    }
    if (sakura.getDiscs().stream().anyMatch(d -> d.getId().equals(discId))) {
        if (LOGGER.isInfoEnabled()) {
            infoRequest("[添加碟片到列表失败][指定的碟片已存在于列表][ASIN={}][列表={}]", disc.getAsin(), sakura.getTitle());
        }
        return errorMessage("指定的碟片已存在于列表");
    }
    sakura.getDiscs().add(disc);
    if (LOGGER.isInfoEnabled()) {
        infoRequest("[添加碟片到列表成功][ASIN={}][列表={}]", disc.getAsin(), sakura.getTitle());
    }
    return objectResult(disc.toJSON(getColumns(discColumns)));
}
Also used : Disc(mingzuozhibi.persist.disc.Disc) Sakura(mingzuozhibi.persist.disc.Sakura) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

Sakura (mingzuozhibi.persist.disc.Sakura)13 Transactional (org.springframework.transaction.annotation.Transactional)8 Disc (mingzuozhibi.persist.disc.Disc)6 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)6 JSONObject (org.json.JSONObject)5 LocalDateTime (java.time.LocalDateTime)4 UpdateType (mingzuozhibi.persist.disc.Disc.UpdateType)3 ViewType (mingzuozhibi.persist.disc.Sakura.ViewType)3 SakuraHelper.noExpiredSakura (mingzuozhibi.support.SakuraHelper.noExpiredSakura)3 LocalDate (java.time.LocalDate)2 java.util (java.util)2 Collectors (java.util.stream.Collectors)2 Stream (java.util.stream.Stream)2 Dao (mingzuozhibi.support.Dao)2 Criteria (org.hibernate.Criteria)2 Restrictions (org.hibernate.criterion.Restrictions)2 JSONArray (org.json.JSONArray)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2 Autowired (org.springframework.beans.factory.annotation.Autowired)2