git_brcal/src/brecal.model/Ship.cs

146 lines
4.6 KiB
C#

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace brecal.model
{
public class Ship : DbEntity
{
#region Properties
public string? Name { get; set; }
public int? IMO { get; set; }
public string? Callsign { get; set; }
public uint? Participant_Id { get; set; }
public Participant? Participant { get; set; }
public double? Length { get; set; }
public double? Width { get; set; }
public bool IsTug { get { return Participant_Id != null; } }
public string? TugCompany
{
get { if (Participant != null) return Participant.Name; else return ""; }
}
#endregion
#region public static methods
public static async Task<List<Ship>> LoadAll(IDBManager manager)
{
List<DbEntity> loadResultList = await manager.Load(SetLoadQuery, LoadElems);
List<Ship> result = new();
foreach (Ship s in loadResultList.Cast<Ship>())
result.Add(s);
return result;
}
public static void SetLoadQuery(IDbCommand cmd, params object?[] list)
{
cmd.CommandText = "SELECT id, name, imo, callsign, participant_id, length, width, created, modified FROM ship";
}
public static List<DbEntity> LoadElems(IDataReader reader)
{
List<DbEntity> result = new List<DbEntity>();
while (reader.Read())
{
Ship s = new();
s.Id = (uint)reader.GetInt32(0);
if (!reader.IsDBNull(1)) s.Name = reader.GetString(1);
if (!reader.IsDBNull(2)) s.IMO = reader.GetInt32(2);
if (!reader.IsDBNull(3)) s.Callsign = reader.GetString(3);
if (!reader.IsDBNull(4)) s.Participant_Id = (uint)reader.GetInt32(4);
if (!reader.IsDBNull(5)) s.Length = reader.GetFloat(5);
if (!reader.IsDBNull(6)) s.Width = reader.GetFloat(6);
if (!reader.IsDBNull(7)) s.Created = reader.GetDateTime(7);
if (!reader.IsDBNull(8)) s.Modified = reader.GetDateTime(8);
result.Add(s);
}
return result;
}
#endregion
#region DbEntity implementation
public override void SetCreate(IDbCommand cmd)
{
cmd.CommandText = "INSERT INTO ship (name, imo, callsign, participant_id, length, width) VALUES ( @NAME, @IMO, @CALLSIGN, @PID, @LENGTH, @WIDTH)";
this.SetParameters(cmd);
}
public override void SetDelete(IDbCommand cmd)
{
cmd.CommandText = "DELETE FROM ship WHERE id = @ID";
IDataParameter idParam = cmd.CreateParameter();
idParam.ParameterName = "ID";
idParam.Value = this.Id;
cmd.Parameters.Add(idParam);
}
public override void SetUpdate(IDbCommand cmd)
{
cmd.CommandText = "UPDATE ship SET name = @NAME, imo = @IMO, callsign = @CALLSIGN, participant_id = @PID, length = @LENGTH, widht = @WIDTH WHERE id = @ID";
this.SetParameters(cmd);
}
#endregion
#region private methods
private void SetParameters(IDbCommand cmd)
{
IDbDataParameter id = cmd.CreateParameter();
id.ParameterName = "ID";
id.Value = this.Id;
cmd.Parameters.Add(id);
IDbDataParameter pid = cmd.CreateParameter();
pid.ParameterName = "PID";
pid.Value = this.Participant_Id;
cmd.Parameters.Add(pid);
IDbDataParameter name = cmd.CreateParameter();
name.ParameterName = "NAME";
name.Value = this.Name;
cmd.Parameters.Add(name);
IDbDataParameter imoparam = cmd.CreateParameter();
imoparam.ParameterName = "IMO";
imoparam.Value = this.IMO;
cmd.Parameters.Add(imoparam);
IDataParameter callsign = cmd.CreateParameter();
callsign.ParameterName = "CALLSIGN";
callsign.Value = this.Callsign;
cmd.Parameters.Add(callsign);
IDataParameter length = cmd.CreateParameter();
length.ParameterName = "LENGTH";
length.Value = this.Length;
cmd.Parameters.Add(length);
IDataParameter width = cmd.CreateParameter();
width.ParameterName = "WIDTH";
width.Value = this.Width;
cmd.Parameters.Add(width);
}
#endregion
}
}