Browse Source

news update fix 3

master
gsd 3 months ago
parent
commit
9fe65e6418
  1. 20
      src/main/java/app/controllers/bot/DiscordNewsController.java
  2. 4
      src/main/java/app/entities/db/Annonce.java
  3. 7
      src/main/java/app/repositories/AnnonceRepository.java

20
src/main/java/app/controllers/bot/DiscordNewsController.java

@ -6,10 +6,7 @@ import app.entities.db.Annonce;
import app.repositories.AnnonceRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("api/bot/discord")
@ -21,9 +18,24 @@ public class DiscordNewsController {
@PostMapping("/news")
@CheckWebAccess(auth_method = AuthMethod.SECRET_KEY)
public ResponseEntity createNews(@RequestBody Annonce annonce) {
if (annonceRepository.getAnnonceByUniq(annonce.getUniq()) != null)
return ResponseEntity.badRequest().build();
annonce.setId(null);
annonce.setType("news");
annonceRepository.save(annonce);
return ResponseEntity.ok().body(null);
}
@DeleteMapping("/news")
@CheckWebAccess(auth_method = AuthMethod.SECRET_KEY)
public ResponseEntity deleteNews(@RequestBody String uniq) {
Annonce annonce = annonceRepository.getAnnonceByUniq(uniq);
if (annonce == null)
return ResponseEntity.notFound().build();
else {
annonceRepository.delete(uniq);
return ResponseEntity.ok(null);
}
}
}

4
src/main/java/app/entities/db/Annonce.java

@ -47,6 +47,10 @@ public class Annonce {
@JsonIgnore
private Boolean deleted;
@Column(name = "uniq")
@JsonIgnore
private String uniq;
public String[] getAttachments() {
return attachments != null?attachments.split("\n"):new String[]{};
}

7
src/main/java/app/repositories/AnnonceRepository.java

@ -5,6 +5,7 @@ import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;
public interface AnnonceRepository extends PagingAndSortingRepository<Annonce, Long> {
@Query(value = "select n from Annonce n where n.type like 'news' and n.deleted = false order by n.id desc")
@ -13,5 +14,11 @@ public interface AnnonceRepository extends PagingAndSortingRepository<Annonce, L
@Query(value = "select n from Annonce n where n.type like 'reason4play' and n.deleted = false order by n.id desc")
Page<Annonce> getAnnonce(Pageable pageable);
@Query(value = "select n from Annonce n where n.uniq like :uniq and n.deleted = false")
Annonce getAnnonceByUniq(@Param(value = "uniq") String uniq);
void save(Annonce annonce);
@Query(value = "update Annonce n set n.deleted = true where n.uniq like :uniq")
void delete(@Param(value = "uniq") String uniq);
}

Loading…
Cancel
Save