API - Date Fields

The issue was the database had a date type column. I had problems getting the angular program to send a date in the proper format. It was so problematic that I converted the data type of the date column to a string. That is my entity object had a datatype of DateTime. My view model object had a data type of string. The Angular integration started working.

One complexity that occurred was when the Angular program would send a date (ex 12/1/2022) the value would be converted to 12/1/2022 12:00 (greenwich time?), by the time it ended up in the database it was recording 11/30/2022. The fix was modifying the mapping object.

Here is a sample

The Entity object (Sample)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
using System;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using System.Collections.Generic;
using x.API.BankAccount;
using x.API.Vendor;


using x.API.Base;
namespace x.API.Transaction
{
public class TransactionEntity : BaseEntity
{
public TransactionEntity()
{
AccountBalance = 0;
:
TheDate = new DateTime();
}

public decimal AccountBalance { get; set; }
:
[Required]
public DateTime TheDate { get; set; }
:
[ForeignKey("Vendor")]
public long? VendorID { get; set; }
public virtual VendorEntity VendorEntity { get; set; }
}
}

The ViewModel

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
using System.ComponentModel.DataAnnotations;
using System.Collections.Generic;
using x.API.BankAccount;
using x.API.Vendor;

using x.API.Base;
namespace x.API.Transaction
{

public class TransactionViewModel : BaseEntityVM
{
public TransactionViewModel()
{
AccountBalance = 0;
: TheDate = null;
}

public float AccountBalance { get; set; }

:
[Required(ErrorMessage = "TheDate is Required")]
public string TheDate { get; set; }

:
public VendorViewModel Vendor { get; set; }

}
}

The Mapping object

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
using AutoMapper;
using Microsoft.AspNetCore.Routing.Constraints;
using System.Globalization;

namespace x.API.Transaction
{
public class TransactionMapping : Profile
{
public TransactionMapping()
{
CreateMap<TransactionViewModel, TransactionEntity>()
.ForMember(dest => dest.TheDate,
// theDate is a string - sample: 2022-12-01T00:00:00.000Z
opt => opt.MapFrom(s => DateTime.ParseExact(s.TheDate.Substring(0,10), "yyyy-MM-dd", CultureInfo.InvariantCulture)) )
.ForMember(dest => dest.VendorEntity,
opt => opt.MapFrom(src => src.Vendor))
.ReverseMap();
}
}
}

This is the model on the angular side

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import { BaseEntity } from '../shared/models';
import { BankAccount } from '../bank-account/bank-account.model';
import { Vendor } from '../vendor/vendor.model';


export class Transaction extends BaseEntity {
constructor (
accountBalance: number=0
:
, vendor: Vendor | null = null
:
, theDate: Date | null
:
) {
super(id);
this.accountBalance=accountBalance;
:
this.vendor=vendor;
:
this.theDate=theDate!;
:
}

accountBalance: number | null;
:
// vendorID: number;
vendor: Vendor | null;
:
theDate: Date;
:
}