PIbd-23_Abazov_A.A._Constru.../ConstructionCompany/ConstructionCompanyPsqlImplement/Models/Order.cs
2023-05-11 13:03:42 +04:00

129 lines
4.4 KiB
C#

using ConstructionCompanyContracts.BindingModels;
using ConstructionCompanyContracts.ViewModels;
using ConstructionCompanyDataModels.Enums;
using ConstructionCompanyDataModels.Models;
using MongoDB.Bson;
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 BsonDocument CreateBSON =>
new BsonDocument
{
{"_id", Id},
{"description", $"{Description}"},
{"adress", $"{Adress}"},
{"price", Price},
{"status", $"{Status}"},
{"customerNumber", $"{CustomerNumber}"},
{"dateBegin", new BsonDateTime(DateBegin)},
{"dateEnd", DateEnd.HasValue ? new BsonDateTime(DateEnd.Value) : ""},
{"employeesId", new BsonArray() },
{"materials", new BsonArray()}
};
public BsonDocument UpdateBSON(BsonArray employeesId, BsonArray materials) =>
new BsonDocument
{
{"_id", Id - 1},
{"description", $"{Description}"},
{"adress", $"{Adress}"},
{"price", Price},
{"status", $"{Status}"},
{"customerNumber", $"{CustomerNumber}"},
{"dateBegin", new BsonDateTime(DateBegin)},
{"dateEnd", DateEnd.HasValue ? new BsonDateTime(DateEnd.Value) : ""},
{"employeesId", employeesId },
{"materials", materials}
};
public OrderViewModel GetViewModel => new()
{
Id = Id,
Description = Description,
Adress = Adress,
CustomerNumber = CustomerNumber,
Price = Price,
Status = Status,
DateBegin = DateBegin,
DateEnd = DateEnd
};
}
}