84 lines
1.9 KiB
C#
Raw Normal View History

2024-04-15 13:34:57 -05:00
#nullable enable
#region
using System;
using System.ComponentModel.DataAnnotations;
using System.Diagnostics.CodeAnalysis;
using System.Text.RegularExpressions;
using website.Models.events;
#endregion
namespace website.Models {
/// <summary>
/// This method is used due to DateTime and TimeSpan
/// being difficult to send in a post request.
/// </summary>
public class PostEvent {
private string _date;
private string? _time;
public int? eventId { get; set; }
[NotNull] public string? location { get; set; }
/// <summary>
/// YYYY/MM/DD
/// </summary>
[NotNull]
[Required]
public string date {
get => _date;
set {
if (Utilities.verifyDate(value)) {
_date = value;
return;
}
throw new ArgumentException("Date in incorrect format or null");
}
}
/// <summary>
/// HH:MM:SS
/// </summary>
public string? time {
get => _time;
set {
if (value == null) {
_time = null;
return;
}
if (Utilities.verifyTime(value)) ;
_time = value;
}
}
public string? color { get; set; }
public string? textColor { get; set; }
public string? imageUri { get; set; }
public string? description { get; set; }
public static Event postEventToEvent(PostEvent postEvent) {
if (postEvent.time == null)
return new Event {
eventId = postEvent.eventId,
location = postEvent.location,
date = Utilities.stringDateToDateTime(postEvent.date),
time = null
};
Match match = new Regex("^[0-9]{2}$").Match(postEvent.time);
int hours = int.Parse(match.Value);
int minutes = int.Parse(match.NextMatch().Value);
int seconds = int.Parse(match.NextMatch().Value);
return new Event {
eventId = postEvent.eventId,
location = postEvent.location,
date = Utilities.stringDateToDateTime(postEvent.date),
time = Utilities.stringTimeToTimeSpan(postEvent.time)
};
}
}
}