Zapisywanie stanu aktualnego po użyciu operacji CRUD

0

Witam,

Chciałbym by w moim projekcie po wykonaniu jakiejkolwiek operacji CRUD (np. w Postmanie) zapisywał się stan obecny danych (aby po wyłączeniu i ponownym włączeniu projektu mieć aktualne wszystkie zmiany dokonane wcześniej). Uprzejmie proszę o podpowiedź.

Projekt przesyłam w załączniku

0

To zapisuj te dane do jakiegoś pliku albo bazy danych.

0

Nie chcę używać bazy danych. W jaki sposób zapisać je w pliku?

0

w jaki sobie wymyślisz. Ne chcesz używać DB istniejących to sobie napisz własną, bo "zapisywanie danych do pliku", wraz z ich odczytem, to właśnie podstawowe zadanie BD

0

Zmieniłem trochę projekt i jestem już blisko osiągnięcia zamierzonego rezultatu.
Stworzyłem 2 funkcje: do zapisu danych w pliku i do ich odczytu. Zapis działa prawidłowo (na zasadzie: object -> string -> byte[] -> stream).
Jedynym problemem pozostaje dla mnie dokończenie funkcji odczytu, nie wiem jak poprawnie zdeserializować tablicę jsonów do listy i podstawić ją pod miejsce właściwej listy rezerwacji (w klasie repozytorium). Uprzejmie proszę o pomoc.

 

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Web;

namespace WebServices.Models
{
    public class ReservationRepository : IReservationRepository
    {

        public void SaveList()
        {
           // FileStream stream = new FileStream(@"C:\Users\Public\Testfolder\persons.dat", FileMode.Create);
            try
            {
                string json = JsonConvert.SerializeObject(data);
                File.WriteAllText(@"C:\Users\Public\Testfolder\Json2.txt", json);
                byte[] array = Encoding.ASCII.GetBytes(json);
                MemoryStream ms = new MemoryStream(array);
                FileStream file = new FileStream(@"C:\Users\Public\Testfolder\file.bin", FileMode.Create, FileAccess.Write);
                ms.WriteTo(file);
                file.Close();
                ms.Close();

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

        }

        public void ReadList()
        {
            try
            {

                using (FileStream fsSource = new FileStream(@"C:\Users\Public\Testfolder\file.bin",
                    FileMode.Open, FileAccess.Read))
                {

                    // Read the source file into a byte array.
                    byte[] bytes = new byte[fsSource.Length];
                    int numBytesToRead = (int)fsSource.Length;
                    int numBytesRead = 0;
                    while (numBytesToRead > 0)
                    {
                        // Read may return anything from 0 to numBytesToRead.
                        int n = fsSource.Read(bytes, numBytesRead, numBytesToRead);

                        // Break when the end of the file is reached.
                        if (n == 0)
                            break;

                        numBytesRead += n;
                        numBytesToRead -= n;
                    }
                    numBytesToRead = bytes.Length;
                    string json = Encoding.UTF8.GetString(bytes, 0, bytes.Length);
                    File.WriteAllText(@"C:\Users\Public\Testfolder\Json3.txt", json);
                    List<Reservation> res = JsonConvert.DeserializeObject<List<Reservation>>(json);

                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        public List<Reservation> data = new List<Reservation> {
new Reservation {ReservationId = 1, ClientName = "Kowalski",
Location = "Londyn"},
new Reservation {ReservationId = 2, ClientName = "Nowak",
Location = "Nowy Jork"},
new Reservation {ReservationId = 3, ClientName = "Bobrowska",
Location = "Paryż"},
};
        private static ReservationRepository repo = new ReservationRepository();
        public static IReservationRepository getRepository()
        {
            return repo;
        }
        public IEnumerable<Reservation> GetAll()
        {
            ReadList();
            return data;
        }
        public Reservation Get(int id)
        {
            var matches = data.Where(r => r.ReservationId == id);
            
            return matches.Count() > 0 ? matches.First() : null;
        }
        public Reservation Add(Reservation item)
        {
            item.ReservationId = data.Count + 1;
            data.Add(item);
            SaveList();
            return item;
        }
        public void Remove(int id)
        {
            Reservation item = Get(id);
            if (item != null)
            {
                data.Remove(item);
            }
        }
        public bool Update(Reservation item)
        {
            Reservation storedItem = Get(item.ReservationId);
            if (storedItem != null)
            {
                storedItem.ClientName = item.ClientName;
                storedItem.Location = item.Location;
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}

1 użytkowników online, w tym zalogowanych: 0, gości: 1