using ConstructionCompanyContracts.BindingModels; using ConstructionCompanyContracts.ViewModels; using ConstructionCompanyDataModels.Enums; using ConstructionCompanyDataModels.Models; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConstructionCompanyPsqlImplement.Models { public class Order : IOrderModel { public int Id { get; private set; } public string Description { get; private set; } = string.Empty; public string Adress { get; private set; } = string.Empty; public string CustomerNumber { get; private set; } = string.Empty; public double Price { get; private set; } public OrderStatus Status { get; private set; } public DateTime DateBegin { get; private set; } public DateTime? DateEnd { get; private set; } public static Order? Create(OrderBindingModel? model) { if (model == null) { return null; } return new Order() { Id = model.Id, Description = model.Description, Adress = model.Adress, CustomerNumber = model.CustomerNumber, Price = model.Price, Status = model.Status, DateBegin = model.DateBegin, DateEnd = model.DateEnd, }; } public void Update(OrderBindingModel? model) { if (model == null) { return; } Status = model.Status; if (model.DateEnd.HasValue) DateEnd = model.DateEnd; } public static string CreateCommand(OrderBindingModel? model) { if (model == null) { return ""; } return $"INSERT INTO \"order\"(description, adress, price, status, " + $"customer_number, date_begin, date_end) VALUES(\'{model.Description}\', \'{model.Adress}\', {model.Price}, " + $"\'{model.Status}\', \'{model.CustomerNumber}\', \'{model.DateBegin}\', " + $"{(model.DateEnd.HasValue ? $"\'{model.DateEnd}\'" : "null")});"; } public static string UpdateCommand(OrderBindingModel? model) { if (model == null) { return ""; } return $"UPDATE \"order\" SET status = \'{model.Status}\', date_end = {(model.DateEnd.HasValue ? $"\'{model.DateEnd}\'" : "null")} " + $"WHERE id = {model.Id}"; } public static string DeleteCommand(OrderBindingModel? model) { if (model == null) { return ""; } return $"DELETE FROM \"order\" WHERE id = {model.Id}"; } public OrderViewModel GetViewModel => new() { Id = Id, Description = Description, Adress = Adress, CustomerNumber = CustomerNumber, Price = Price, Status = Status, DateBegin = DateBegin, DateEnd = DateEnd }; } }