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

97 lines
2.8 KiB
C#

using ConstructionCompanyContracts.BindingModels;
using ConstructionCompanyContracts.ViewModels;
using ConstructionCompanyDataModels.Models;
using MongoDB.Bson;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
namespace ConstructionCompanyPsqlImplement.Models
{
public class Employee : IEmployeeModel
{
public string EmployeeName { get; private set; } = string.Empty;
public int Id { get; private set; }
public int PositionID { get; private set; }
public Position Position { get; set; } = new();
public static Employee? Create(EmployeeBindingModel? model, List<Position> positions)
{
if (model == null)
{
return null;
}
return new Employee()
{
Id = model.Id,
EmployeeName = model.EmployeeName,
PositionID = model.PositionID,
Position = positions.First(x => x.Id == model.PositionID)
};
}
public void Update(EmployeeBindingModel? model)
{
if (model == null)
{
return;
}
EmployeeName = model.EmployeeName;
PositionID = model.PositionID;
}
public static string CreateCommand(EmployeeBindingModel? model)
{
if (model == null)
{
return "";
}
return $"INSERT INTO employee(name, position_id) VALUES(\'{model.EmployeeName}\', {model.PositionID});";
}
public static string UpdateCommand(EmployeeBindingModel? model)
{
if (model == null)
{
return "";
}
return $"UPDATE employee SET \"name\" = \'{model.EmployeeName}\', position_id = {model.PositionID} WHERE id = {model.Id}";
}
public static string DeleteCommand(EmployeeBindingModel? model)
{
if (model == null)
{
return "";
}
return $"DELETE FROM employee WHERE id = {model.Id}";
}
public EmployeeViewModel GetViewModel => new()
{
Id = Id,
EmployeeName = EmployeeName,
PositionID = PositionID,
PositionName = Position.PositionName
};
public BsonDocument CreateBSON =>
new BsonDocument
{
{"_id", Id - 1},
{"EmployeeName", $"{EmployeeName}"},
{"positionId", PositionID - 1}
};
public BsonDocument UpdateBSON =>
new BsonDocument
{
{"_id", Id - 1},
{"EmployeeName", $"{EmployeeName}"},
{"positionId", PositionID - 1}
};
}
}