BusStopDs.cs 3.49 KB
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using MapsDb.Interfaces;
using MapsDb.Models;
using MapsModels.DsModels;
using Microsoft.EntityFrameworkCore;
namespace MapsDb.DataService
{
    public class BusStopDs : IBusStopDs
    {
        private PostgresDbContext _context;
        public BusStopDs(){
            _context = new PostgresDbContext();
        }
        public Task<IList<BusStopListDsM>> GetIndexListAsync(){
            return Task.Factory.StartNew(GetAllBusStop);
        }
        private IList<BusStopListDsM> GetAllBusStop()
        {
           return _context.BusStop.Select(x => new BusStopListDsM
            {
                Road = x.Road.Name, 
                Region = x.Region.Name,
                Settlement = x.Settlement.Name,
                LocationLeft = x.LocationLeft,
                LocationRight = x.LocationRight,
                StateCommon = x.StateCommon.Value
            }).ToList();
        }

        public Task SaveAsync(BusStopEditDsM busStop, int? id = null){
              return Task.Factory.StartNew(()=> { Save(busStop, id); });
        }
        private void Save(BusStopEditDsM busStop, int? id)
        {   
            BusStop Bs = new BusStop{
                RoadId = busStop.RoadId,
                RegionId = busStop.RegionId,
                SettlementId = busStop.SettlementId,
                LocationLeft = busStop.LocationLeft,
                LocationRight = busStop.LocationRight,
                SurfaceTypeId = busStop.SurfaceTypeId,
                AreaStopAvailability = busStop.AreaStopAvailability,
                AreaLandAvailability = busStop.AreaLandAvailability,
                PocketAvailability = busStop.PocketAvailability,
                ToiletAvailability = busStop.ToiletAvailability,
                YearBuild = busStop.YearBuild,
                YearRepair = busStop.YearRepair,
                StateCommonId = busStop.StateCommonId
            };
            var busStopFromDb = _context.BusStop.SingleOrDefault(x => x.BusStopId == id);
            if(busStopFromDb != null)
            {
                busStopFromDb = Bs;
            }
            else
            {
                _context.BusStop.Add(Bs);
            }
            _context.SaveChanges();
        }
        public Task<BusStopDetailsDsM> FindOneDetailsAsync(int Id){
             return Task.Factory.StartNew(()=> { return FindOneDetails(Id); });
        }
        private BusStopDetailsDsM FindOneDetails(int Id){
           return _context.BusStop.Where(x => x.BusStopId == Id).Select(x => new BusStopDetailsDsM{
                BusStopId = x.BusStopId,
                Road = x.Road.Name, 
                Region = x.Region.Name,
                Settlement = x.Settlement.Name,
                LocationLeft = x.LocationLeft,
                LocationRight = x.LocationRight,
                StateCommon = x.StateCommon.Value,
                AreaStopAvailability = x.AreaStopAvailability,
                AreaLandAvailability = x.AreaLandAvailability,
                PocketAvailability = x.PocketAvailability,
                ToiletAvailability = x.ToiletAvailability,
                YearBuild = x.YearBuild,
                YearRepair = x.YearRepair
           }).Single();
        }
        public async Task<int> DeleteAsync(int? Id)
        {
            var busStop = await _context.BusStop.SingleOrDefaultAsync(x => x.BusStopId == Id);
            _context.BusStop.Remove(busStop);
            return await _context.SaveChangesAsync();
        }
    }
}