Доделал implements
This commit is contained in:
parent
2a460af5cc
commit
9bf56d579d
@ -9,6 +9,6 @@ namespace HotelContracts.SearchModels
|
|||||||
public class WorkerSearchModel
|
public class WorkerSearchModel
|
||||||
{
|
{
|
||||||
public int? Id { get; set; }
|
public int? Id { get; set; }
|
||||||
public int? FIO { get; set; }
|
public string? FIO { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
27
Hotel/HotelDatabaseImplement/HotelDatabase.cs
Normal file
27
Hotel/HotelDatabaseImplement/HotelDatabase.cs
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using HotelDatabaseImplement.Models;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace HotelDatabaseImplement
|
||||||
|
{
|
||||||
|
public class HotelDatabase : DbContext
|
||||||
|
{
|
||||||
|
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
||||||
|
{
|
||||||
|
if (optionsBuilder.IsConfigured == false)
|
||||||
|
{
|
||||||
|
optionsBuilder.UseSqlServer(@"Data Source=localhost\SQLEXPRESS01;Initial Catalog=HotelDatabaseBD;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True");
|
||||||
|
}
|
||||||
|
base.OnConfiguring(optionsBuilder);
|
||||||
|
}
|
||||||
|
public virtual DbSet<Booking> Bookings { get; set; }
|
||||||
|
public virtual DbSet<Client> Clients { get; set; }
|
||||||
|
public virtual DbSet<Post> Posts { get; set; }
|
||||||
|
public virtual DbSet<Room> Rooms { get; set; }
|
||||||
|
public virtual DbSet<Worker> Workers { get; set; }
|
||||||
|
}
|
||||||
|
}
|
@ -1,6 +1,8 @@
|
|||||||
using HotelContracts.SearchModels;
|
using HotelContracts.BindingModels;
|
||||||
|
using HotelContracts.SearchModels;
|
||||||
using HotelContracts.StoragesContracts;
|
using HotelContracts.StoragesContracts;
|
||||||
using HotelContracts.ViewModels;
|
using HotelContracts.ViewModels;
|
||||||
|
using HotelDatabaseImplement.Models;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
@ -12,6 +14,79 @@ namespace HotelDatabaseImplement.Implements
|
|||||||
{
|
{
|
||||||
public class BookingStorage : IBookingStorage
|
public class BookingStorage : IBookingStorage
|
||||||
{
|
{
|
||||||
|
public BookingViewModel? Delete(BookingBindingModel model)
|
||||||
|
{
|
||||||
|
using var context = new HotelDatabase();
|
||||||
|
var element = context.Bookings.FirstOrDefault(x => x.Id == model.Id);
|
||||||
|
if (element != null)
|
||||||
|
{
|
||||||
|
context.Bookings.Remove(element);
|
||||||
|
context.SaveChanges();
|
||||||
|
return element.GetViewModel;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public BookingViewModel? GetElement(BookingSearchModel model)
|
||||||
|
{
|
||||||
|
if (!model.Id.HasValue)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
using var context = new HotelDatabase();
|
||||||
|
return context.Bookings.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<BookingViewModel> GetFilteredList(BookingSearchModel model)
|
||||||
|
{
|
||||||
|
if (!model.Id.HasValue)
|
||||||
|
{
|
||||||
|
return new();
|
||||||
|
}
|
||||||
|
using var context = new HotelDatabase();
|
||||||
|
return context.Bookings.Where(x => x.Id == model.Id).Select(x => x.GetViewModel).ToList();
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<BookingViewModel> GetFullList()
|
||||||
|
{
|
||||||
|
using var context = new HotelDatabase();
|
||||||
|
return context.Bookings.Select(x => x.GetViewModel).ToList();
|
||||||
|
}
|
||||||
|
|
||||||
|
public BookingViewModel? Insert(BookingBindingModel model)
|
||||||
|
{
|
||||||
|
var newBooking = Booking.Create(model);
|
||||||
|
if (newBooking == null)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
using var context = new HotelDatabase();
|
||||||
|
context.Bookings.Add(newBooking);
|
||||||
|
context.SaveChanges();
|
||||||
|
return newBooking.GetViewModel;
|
||||||
|
}
|
||||||
|
|
||||||
|
public BookingViewModel? Update(BookingBindingModel model)
|
||||||
|
{
|
||||||
|
using var context = new HotelDatabase();
|
||||||
|
using var transaction = context.Database.BeginTransaction();
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var booking = context.Bookings.FirstOrDefault(x => x.Id == model.Id);
|
||||||
|
if (booking == null)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
booking.Update(model);
|
||||||
|
context.SaveChanges();
|
||||||
|
transaction.Commit();
|
||||||
|
return booking.GetViewModel;
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
transaction.Rollback();
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,8 @@
|
|||||||
using HotelContracts.StoragesContracts;
|
using HotelContracts.BindingModels;
|
||||||
|
using HotelContracts.SearchModels;
|
||||||
|
using HotelContracts.StoragesContracts;
|
||||||
|
using HotelContracts.ViewModels;
|
||||||
|
using HotelDatabaseImplement.Models;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
@ -9,5 +13,69 @@ namespace HotelDatabaseImplement.Implements
|
|||||||
{
|
{
|
||||||
public class ClientStorage : IClientStorage
|
public class ClientStorage : IClientStorage
|
||||||
{
|
{
|
||||||
|
public ClientViewModel? Delete(ClientBindingModel model)
|
||||||
|
{
|
||||||
|
using var context = new HotelDatabase();
|
||||||
|
var element = context.Clients.FirstOrDefault(x => x.Id == model.Id);
|
||||||
|
if (element != null)
|
||||||
|
{
|
||||||
|
context.Clients.Remove(element);
|
||||||
|
context.SaveChanges();
|
||||||
|
return element.GetViewModel;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ClientViewModel? GetElement(ClientSearchModel model)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrEmpty(model.Name) && !model.Id.HasValue)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
using var context = new HotelDatabase();
|
||||||
|
return context.Clients.FirstOrDefault(x => (!string.IsNullOrEmpty(model.Name)) && x.Name == model.Name || model.Id.HasValue && x.Id == model.Id)?.GetViewModel;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<ClientViewModel> GetFilteredList(ClientSearchModel model)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrEmpty(model.Name))
|
||||||
|
{
|
||||||
|
return new();
|
||||||
|
}
|
||||||
|
using var context = new HotelDatabase();
|
||||||
|
return context.Clients.Where(x => x.Name.Contains(model.Name)).Select(x => x.GetViewModel).ToList();
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<ClientViewModel> GetFullList()
|
||||||
|
{
|
||||||
|
using var context = new HotelDatabase();
|
||||||
|
return context.Clients.Select(x => x.GetViewModel).ToList();
|
||||||
|
}
|
||||||
|
|
||||||
|
public ClientViewModel? Insert(ClientBindingModel model)
|
||||||
|
{
|
||||||
|
var newClient = Client.Create(model);
|
||||||
|
if (newClient == null)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
using var context = new HotelDatabase();
|
||||||
|
context.Clients.Add(newClient);
|
||||||
|
context.SaveChanges();
|
||||||
|
return newClient.GetViewModel;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ClientViewModel? Update(ClientBindingModel model)
|
||||||
|
{
|
||||||
|
using var context = new HotelDatabase();
|
||||||
|
var component = context.Clients.FirstOrDefault(x => x.Id == model.Id);
|
||||||
|
if (component == null)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
component.Update(model);
|
||||||
|
context.SaveChanges();
|
||||||
|
return component.GetViewModel;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,8 @@
|
|||||||
using HotelContracts.StoragesContracts;
|
using HotelContracts.BindingModels;
|
||||||
|
using HotelContracts.SearchModels;
|
||||||
|
using HotelContracts.StoragesContracts;
|
||||||
|
using HotelContracts.ViewModels;
|
||||||
|
using HotelDatabaseImplement.Models;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
@ -9,5 +13,69 @@ namespace HotelDatabaseImplement.Implements
|
|||||||
{
|
{
|
||||||
public class PostStorage : IPostStorage
|
public class PostStorage : IPostStorage
|
||||||
{
|
{
|
||||||
|
public PostViewModel? Delete(PostBindingModel model)
|
||||||
|
{
|
||||||
|
using var context = new HotelDatabase();
|
||||||
|
var element = context.Posts.FirstOrDefault(x => x.Id == model.Id);
|
||||||
|
if (element != null)
|
||||||
|
{
|
||||||
|
context.Posts.Remove(element);
|
||||||
|
context.SaveChanges();
|
||||||
|
return element.GetViewModel;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public PostViewModel? GetElement(PostSearchModel model)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrEmpty(model.PostName) && !model.Id.HasValue)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
using var context = new HotelDatabase();
|
||||||
|
return context.Posts.FirstOrDefault(x => (!string.IsNullOrEmpty(model.PostName)) && x.PostName == model.PostName || model.Id.HasValue && x.Id == model.Id)?.GetViewModel;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<PostViewModel> GetFilteredList(PostSearchModel model)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrEmpty(model.PostName))
|
||||||
|
{
|
||||||
|
return new();
|
||||||
|
}
|
||||||
|
using var context = new HotelDatabase();
|
||||||
|
return context.Posts.Where(x => x.PostName.Contains(model.PostName)).Select(x => x.GetViewModel).ToList();
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<PostViewModel> GetFullList()
|
||||||
|
{
|
||||||
|
using var context = new HotelDatabase();
|
||||||
|
return context.Posts.Select(x => x.GetViewModel).ToList();
|
||||||
|
}
|
||||||
|
|
||||||
|
public PostViewModel? Insert(PostBindingModel model)
|
||||||
|
{
|
||||||
|
var newPost = Post.Create(model);
|
||||||
|
if (newPost == null)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
using var context = new HotelDatabase();
|
||||||
|
context.Posts.Add(newPost);
|
||||||
|
context.SaveChanges();
|
||||||
|
return newPost.GetViewModel;
|
||||||
|
}
|
||||||
|
|
||||||
|
public PostViewModel? Update(PostBindingModel model)
|
||||||
|
{
|
||||||
|
using var context = new HotelDatabase();
|
||||||
|
var component = context.Posts.FirstOrDefault(x => x.Id == model.Id);
|
||||||
|
if (component == null)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
component.Update(model);
|
||||||
|
context.SaveChanges();
|
||||||
|
return component.GetViewModel;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,8 @@
|
|||||||
using HotelContracts.StoragesContracts;
|
using HotelContracts.BindingModels;
|
||||||
|
using HotelContracts.SearchModels;
|
||||||
|
using HotelContracts.StoragesContracts;
|
||||||
|
using HotelContracts.ViewModels;
|
||||||
|
using HotelDatabaseImplement.Models;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
@ -9,5 +13,69 @@ namespace HotelDatabaseImplement.Implements
|
|||||||
{
|
{
|
||||||
public class RoomStorage : IRoomStorage
|
public class RoomStorage : IRoomStorage
|
||||||
{
|
{
|
||||||
|
public RoomViewModel? Delete(RoomBindingModel model)
|
||||||
|
{
|
||||||
|
using var context = new HotelDatabase();
|
||||||
|
var element = context.Rooms.FirstOrDefault(x => x.Id == model.Id);
|
||||||
|
if (element != null)
|
||||||
|
{
|
||||||
|
context.Rooms.Remove(element);
|
||||||
|
context.SaveChanges();
|
||||||
|
return element.GetViewModel;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public RoomViewModel? GetElement(RoomSearchModel model)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrEmpty(model.Number.ToString()) && !model.Id.HasValue)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
using var context = new HotelDatabase();
|
||||||
|
return context.Rooms.FirstOrDefault(x => (!string.IsNullOrEmpty(model.Number.ToString())) && x.Number.ToString() == model.Number.ToString() || model.Id.HasValue && x.Id == model.Id)?.GetViewModel;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<RoomViewModel> GetFilteredList(RoomSearchModel model)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrEmpty(model.Number.ToString()))
|
||||||
|
{
|
||||||
|
return new();
|
||||||
|
}
|
||||||
|
using var context = new HotelDatabase();
|
||||||
|
return context.Rooms.Where(x => x.Number.ToString().Contains(model.Number.ToString())).Select(x => x.GetViewModel).ToList();
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<RoomViewModel> GetFullList()
|
||||||
|
{
|
||||||
|
using var context = new HotelDatabase();
|
||||||
|
return context.Rooms.Select(x => x.GetViewModel).ToList();
|
||||||
|
}
|
||||||
|
|
||||||
|
public RoomViewModel? Insert(RoomBindingModel model)
|
||||||
|
{
|
||||||
|
var newRoom = Room.Create(model);
|
||||||
|
if (newRoom == null)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
using var context = new HotelDatabase();
|
||||||
|
context.Rooms.Add(newRoom);
|
||||||
|
context.SaveChanges();
|
||||||
|
return newRoom.GetViewModel;
|
||||||
|
}
|
||||||
|
|
||||||
|
public RoomViewModel? Update(RoomBindingModel model)
|
||||||
|
{
|
||||||
|
using var context = new HotelDatabase();
|
||||||
|
var component = context.Rooms.FirstOrDefault(x => x.Id == model.Id);
|
||||||
|
if (component == null)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
component.Update(model);
|
||||||
|
context.SaveChanges();
|
||||||
|
return component.GetViewModel;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,8 @@
|
|||||||
using HotelContracts.StoragesContracts;
|
using HotelContracts.BindingModels;
|
||||||
|
using HotelContracts.SearchModels;
|
||||||
|
using HotelContracts.StoragesContracts;
|
||||||
|
using HotelContracts.ViewModels;
|
||||||
|
using HotelDatabaseImplement.Models;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
@ -9,5 +13,69 @@ namespace HotelDatabaseImplement.Implements
|
|||||||
{
|
{
|
||||||
public class WorkerStorage : IWorkerStorage
|
public class WorkerStorage : IWorkerStorage
|
||||||
{
|
{
|
||||||
|
public WorkerViewModel? Delete(WorkerBindingModel model)
|
||||||
|
{
|
||||||
|
using var context = new HotelDatabase();
|
||||||
|
var element = context.Workers.FirstOrDefault(x => x.Id == model.Id);
|
||||||
|
if (element != null)
|
||||||
|
{
|
||||||
|
context.Workers.Remove(element);
|
||||||
|
context.SaveChanges();
|
||||||
|
return element.GetViewModel;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public WorkerViewModel? GetElement(WorkerSearchModel model)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrEmpty(model.FIO) && !model.Id.HasValue)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
using var context = new HotelDatabase();
|
||||||
|
return context.Workers.FirstOrDefault(x => (!string.IsNullOrEmpty(model.FIO)) && x.FIO == model.FIO || model.Id.HasValue && x.Id == model.Id)?.GetViewModel;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<WorkerViewModel> GetFilteredList(WorkerSearchModel model)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrEmpty(model.FIO))
|
||||||
|
{
|
||||||
|
return new();
|
||||||
|
}
|
||||||
|
using var context = new HotelDatabase();
|
||||||
|
return context.Workers.Where(x => x.FIO.Contains(model.FIO)).Select(x => x.GetViewModel).ToList();
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<WorkerViewModel> GetFullList()
|
||||||
|
{
|
||||||
|
using var context = new HotelDatabase();
|
||||||
|
return context.Workers.Select(x => x.GetViewModel).ToList();
|
||||||
|
}
|
||||||
|
|
||||||
|
public WorkerViewModel? Insert(WorkerBindingModel model)
|
||||||
|
{
|
||||||
|
var newWorker = Worker.Create(model);
|
||||||
|
if (newWorker == null)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
using var context = new HotelDatabase();
|
||||||
|
context.Workers.Add(newWorker);
|
||||||
|
context.SaveChanges();
|
||||||
|
return newWorker.GetViewModel;
|
||||||
|
}
|
||||||
|
|
||||||
|
public WorkerViewModel? Update(WorkerBindingModel model)
|
||||||
|
{
|
||||||
|
using var context = new HotelDatabase();
|
||||||
|
var component = context.Workers.FirstOrDefault(x => x.Id == model.Id);
|
||||||
|
if (component == null)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
component.Update(model);
|
||||||
|
context.SaveChanges();
|
||||||
|
return component.GetViewModel;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -24,7 +24,7 @@ namespace HotelDatabaseImplement.Models
|
|||||||
public string Password { get; set; } = string.Empty;
|
public string Password { get; set; } = string.Empty;
|
||||||
|
|
||||||
public int Id { get; set; }
|
public int Id { get; set; }
|
||||||
public static Client Create(HotelDatabase context, ClientBindingModel model)
|
public static Client Create(ClientBindingModel model)
|
||||||
{
|
{
|
||||||
return new Client()
|
return new Client()
|
||||||
{
|
{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user