первая лаба(2)

This commit is contained in:
maxim
2025-09-05 22:29:19 +04:00
parent 12007bd3ea
commit 4a409b63d7
7 changed files with 2 additions and 369 deletions

View File

@@ -1,70 +0,0 @@
package com.example.demo.controller;
import com.example.demo.dto.AlbumDto;
import org.springframework.web.bind.annotation.*;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
@RestController
@RequestMapping("/api")
public class AlbumController {
private final Map<Integer, AlbumDto> albums = new LinkedHashMap<>();
private final AtomicInteger idCounter = new AtomicInteger(1);
public AlbumController() {
// Инициализация пустой коллекцией (как в db.json)
}
@GetMapping("/albums")
public List<AlbumDto> getAllAlbums() {
return new ArrayList<>(albums.values());
}
@GetMapping("/albums/{id}")
public AlbumDto getAlbumById(@PathVariable Integer id) {
return albums.get(id);
}
@PostMapping("/albums")
public AlbumDto createAlbum(@RequestBody AlbumDto album) {
Integer newId = idCounter.getAndIncrement();
album.setId(newId);
albums.put(newId, album);
return album;
}
@PutMapping("/albums/{id}")
public AlbumDto updateAlbum(@PathVariable Integer id, @RequestBody AlbumDto album) {
if (albums.containsKey(id)) {
album.setId(id);
albums.put(id, album);
return album;
}
return null;
}
@PatchMapping("/albums/{id}")
public AlbumDto patchAlbum(@PathVariable Integer id, @RequestBody Map<String, Object> updates) {
AlbumDto existingAlbum = albums.get(id);
if (existingAlbum != null) {
if (updates.containsKey("artistId")) {
existingAlbum.setArtistId((Integer) updates.get("artistId"));
}
if (updates.containsKey("title")) {
existingAlbum.setTitle((String) updates.get("title"));
}
if (updates.containsKey("year")) {
existingAlbum.setYear((Integer) updates.get("year"));
}
albums.put(id, existingAlbum);
return existingAlbum;
}
return null;
}
@DeleteMapping("/albums/{id}")
public boolean deleteAlbum(@PathVariable Integer id) {
return albums.remove(id) != null;
}
}

View File

@@ -1,70 +0,0 @@
package com.example.demo.controller;
import com.example.demo.dto.SongDto;
import org.springframework.web.bind.annotation.*;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
@RestController
@RequestMapping("/api")
public class SongController {
private final Map<Integer, SongDto> songs = new LinkedHashMap<>();
private final AtomicInteger idCounter = new AtomicInteger(1);
public SongController() {
// Инициализация пустой коллекцией (как в db.json)
}
@GetMapping("/songs")
public List<SongDto> getAllSongs() {
return new ArrayList<>(songs.values());
}
@GetMapping("/songs/{id}")
public SongDto getSongById(@PathVariable Integer id) {
return songs.get(id);
}
@PostMapping("/songs")
public SongDto createSong(@RequestBody SongDto song) {
Integer newId = idCounter.getAndIncrement();
song.setId(newId);
songs.put(newId, song);
return song;
}
@PutMapping("/songs/{id}")
public SongDto updateSong(@PathVariable Integer id, @RequestBody SongDto song) {
if (songs.containsKey(id)) {
song.setId(id);
songs.put(id, song);
return song;
}
return null;
}
@PatchMapping("/songs/{id}")
public SongDto patchSong(@PathVariable Integer id, @RequestBody Map<String, Object> updates) {
SongDto existingSong = songs.get(id);
if (existingSong != null) {
if (updates.containsKey("albumId")) {
existingSong.setAlbumId((Integer) updates.get("albumId"));
}
if (updates.containsKey("title")) {
existingSong.setTitle((String) updates.get("title"));
}
if (updates.containsKey("duration")) {
existingSong.setDuration((String) updates.get("duration"));
}
songs.put(id, existingSong);
return existingSong;
}
return null;
}
@DeleteMapping("/songs/{id}")
public boolean deleteSong(@PathVariable Integer id) {
return songs.remove(id) != null;
}
}

View File

@@ -1,49 +0,0 @@
package com.example.demo.dto;
public class AlbumDto {
private Integer id;
private Integer artistId;
private String title;
private Integer year;
public AlbumDto() {}
public AlbumDto(Integer id, Integer artistId, String title, Integer year) {
this.id = id;
this.artistId = artistId;
this.title = title;
this.year = year;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getArtistId() {
return artistId;
}
public void setArtistId(Integer artistId) {
this.artistId = artistId;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Integer getYear() {
return year;
}
public void setYear(Integer year) {
this.year = year;
}
}

View File

@@ -1,49 +0,0 @@
package com.example.demo.dto;
public class SongDto {
private Integer id;
private Integer albumId;
private String title;
private String duration;
public SongDto() {}
public SongDto(Integer id, Integer albumId, String title, String duration) {
this.id = id;
this.albumId = albumId;
this.title = title;
this.duration = duration;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getAlbumId() {
return albumId;
}
public void setAlbumId(Integer albumId) {
this.albumId = albumId;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDuration() {
return duration;
}
public void setDuration(String duration) {
this.duration = duration;
}
}

View File

@@ -1,65 +0,0 @@
{
"artists": [
{
"id": "9daa",
"name": "testlabуцк",
"description": "sadууу",
"epochId": 2,
"countryId": 3
},
{
"id": "98ad",
"name": "ыфвфы",
"description": "выфв",
"epochId": 1,
"countryId": 1
},
{
"id": "dd28",
"name": "аааа",
"description": "ааа",
"epochId": 2,
"countryId": 3
},
{
"id": "b499",
"name": "бб",
"description": "уафв",
"epochId": 1,
"countryId": 3
},
{
"id": "0f62",
"name": "Максим",
"description": "2",
"epochId": 2,
"countryId": 1
}
],
"countries": [
{
"id": "1",
"name": "Россия"
},
{
"id": "2",
"name": "США"
},
{
"id": "3",
"name": "Тайга"
}
],
"epochs": [
{
"id": "1",
"name": "1980-е"
},
{
"id": "2",
"name": "1990-е"
}
],
"albums": [],
"songs": []
}

View File

@@ -22,9 +22,7 @@
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"json-server": "json-server --watch db.json --port 3001",
"dev": "concurrently \"npm start\" \"npm run json-server\""
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
@@ -44,8 +42,5 @@
"last 1 safari version"
]
},
"devDependencies": {
"concurrently": "^9.2.1",
"json-server": "^1.0.0-beta.3"
}
"devDependencies": {}
}

View File

@@ -1,59 +0,0 @@
{
"artists": [
{
"id": "1",
"name": "Гражданская Оборона",
"description": "Советская и российская рок-группа, основанная Егором Летовым.",
"year": 1984,
"country": "Россия"
},
{
"id": "f28f",
"name": "что-то",
"description": "sdad",
"year": 1998,
"country": "Россия"
},
{
"id": "2134",
"name": "бб",
"description": "ццвыф",
"year": 1999,
"country": "Россия"
}
],
"albums": [
{
"id": "1",
"artistId": 1,
"title": "Поганая молодёжь",
"year": 1985
},
{
"id": "2",
"artistId": 1,
"title": "Оптимизм",
"year": 1985
},
{
"id": "3",
"artistId": 2,
"title": "Камнем по голове",
"year": 1996
}
],
"songs": [
{
"id": "1",
"albumId": 1,
"title": "Я бесполезен",
"duration": "2:30"
},
{
"id": "2",
"albumId": 3,
"title": "Кукла колдуна",
"duration": "3:15"
}
]
}