Skip to content

Commit

Permalink
Merge branch 'main' into feat/CourseController-Update
Browse files Browse the repository at this point in the history
  • Loading branch information
DusicaPesic authored Apr 1, 2024
2 parents b0ed147 + 76c87bf commit 8c17d3c
Show file tree
Hide file tree
Showing 3 changed files with 246 additions and 3 deletions.
200 changes: 200 additions & 0 deletions LangLang/DTO/ExamSlotDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
using LangLang.Core.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace LangLang.DTO
{
public class ExamSlotDTO : INotifyPropertyChanged, IDataErrorInfo
{
public ExamSlotDTO() { }
public int Id { get; set; }

private int _courseId;
private int _maxStudents;
private DateTime _examDate;
private string _time;
private int _numberOfStudents;
private DateTime _examDateTime;

public int CourseId
{
get { return _courseId; }
set
{
_courseId = value;
}
}

public string MaxStudents
{
get { return _maxStudents.ToString(); ; }
set
{
if (int.TryParse(value, out int result) && result >= 0)
{
_maxStudents = result;
}
else
{
throw new ArgumentException("Max number of students must be a non-negative integer.");
}
}
}

public DateTime ExamDate
{
get { return _examDate; }
set
{
_examDate = value;
}
}

public int NumberOfStudents
{
get { return _numberOfStudents; }
set
{
_numberOfStudents = value;
}
}

public string Time
{
get { return _time; }
set
{
_time = value;
}
}

public DateTime ExamDateTime
{
get { return _examDateTime; }
set
{
_examDateTime = value;
}
}

private readonly Regex _TimeRegex = new("^(?:[01]\\d|2[0-3]):(?:[0-5]\\d)$");


public string this[string columnName]
{
get
{

if (columnName == "MaxStudents")
{

if (string.IsNullOrEmpty(MaxStudents)) return "Max number of students is required";
if (!int.TryParse(MaxStudents, out int maxStudents) || maxStudents <= 0) return "Must input a positive integer for max number of students.";
else return "";
}

if (columnName == "ExamDate")
{
if (_examDate <= DateTime.Now) return "Please enter a future date.";
if (_examDate == default) return "Exam date is required";
else return "";
}

if (columnName == "ExamTime")
{
if (string.IsNullOrEmpty(Time)) return "Time is required";
if (!_TimeRegex.Match(Time).Success) return "Time must be of format hh:mm .";
else return "";
}
return "";
}
}

private readonly string[] _validatedProperties = { "MaxStudents", "ExamDate", "ExamTime" };

// checks if all properties are valid
public bool IsValid
{
get
{
foreach (var property in _validatedProperties)
{
if (this[property] != null)
return true;
}

return true;
}
}



public ExamSlotDTO(ExamSlot examSlot)
{
this.Id = examSlot.Id;
this.MaxStudents = examSlot.MaxStudents.ToString();
this.NumberOfStudents = examSlot.NumberOfStudents;
this.CourseId = examSlot.CourseId;
this.ExamDate = examSlot.ExamDateTime.Date;
this.Time = examSlot.ExamDateTime.ToString("HH:mm");
this.ExamDateTime = examSlot.ExamDateTime;
}

private DateTime ToDateTime(DateTime date, string time)
{

string[] timeComponents = time.Split(':');

if (timeComponents.Length == 2)
{
// Parse hour component
if (int.TryParse(timeComponents[0], out int hours))
{
// Parse minute component
if (int.TryParse(timeComponents[1], out int minutes))
{
// Create a new DateTime object with the combined date and time components
DateTime combinedDateTime = new DateTime(date.Year, date.Month, date.Day, hours, minutes, 0);

}
else
{
Console.WriteLine("Invalid minute component.");
}
}
else
{
Console.WriteLine("Invalid hour component.");
}
}
else
{
Console.WriteLine("Invalid time format.");
}
return DateTime.Now;
}

public ExamSlot ToExamSlot()
{
return new ExamSlot(Id, _courseId, _maxStudents, ToDateTime(_examDate,_time) , 0);
}

public string Error => throw new NotImplementedException();

public event PropertyChangedEventHandler? PropertyChanged;

protected virtual void OnPropertyChanged(string name)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}

}
}
15 changes: 13 additions & 2 deletions LangLang/View/TutorWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
</Label>

<!--label for exam slots table-->
<Label x:Name="courseLbl" Content="Select course" Grid.Row="0" Grid.Column="1" HorizontalAlignment="Center" Height="24" Margin="0,10,0,0" VerticalAlignment="Top" Width="310" FontStyle="Italic" Foreground="Black" RenderTransformOrigin="0.5,0.5" FontFamily="Arial" FontWeight="Bold" HorizontalContentAlignment="Center">
<Label x:Name="examSlotsLbl" Content="Your Exam Slots" Grid.Row="0" Grid.Column="1" HorizontalAlignment="Center" Height="24" Margin="0,10,0,0" VerticalAlignment="Top" Width="310" FontStyle="Italic" Foreground="Black" RenderTransformOrigin="0.5,0.5" FontFamily="Arial" FontWeight="Bold" HorizontalContentAlignment="Center">
<Label.RenderTransform>
<TransformGroup>
<ScaleTransform/>
Expand Down Expand Up @@ -97,7 +97,18 @@
<Button Content="Search exams" Padding="5" Height="33" Margin="5" BorderBrush="White" Background="#FFFFDBDB" Foreground="#FF514141" FontFamily="Segoe UI Black" />
</StackPanel>
</Border>
<DataGrid Grid.Column="1" Margin="15,45,15,0" d:ItemsSource="{d:SampleData ItemCount=5}"/>
<DataGrid Grid.Column="1" Margin="15,45,15,0" AutoGenerateColumns="False"
x:Name="ExamSlotsDataGrid"
ItemsSource="{Binding ExamSlots, UpdateSourceTrigger=PropertyChanged}"
SelectedItem="{Binding SelectedExamSlot, UpdateSourceTrigger=PropertyChanged}"
SelectionMode="Single" IsReadOnly="True">
<DataGrid.Columns>
<DataGridTextColumn Header="Course ID" Binding="{Binding CourseId}"/>
<DataGridTextColumn Header="Max Students" Binding="{Binding MaxStudents}"/>
<DataGridTextColumn Header="Number of Students" Binding="{Binding NumberOfStudents}"/>
<DataGridTextColumn Header="Date and Time" Binding="{Binding ExamDateTime, StringFormat={}{0:MM/dd/yyyy HH:mm}}"/>
</DataGrid.Columns>
</DataGrid>
<DataGrid Grid.Column="0" Margin="15,45,15,0" d:ItemsSource="{d:SampleData ItemCount=5}"/>
</Grid>
</Window>
34 changes: 33 additions & 1 deletion LangLang/View/TutorWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
using LangLang.Core.Controller;
using LangLang.Core.Model;

using LangLang.DTO;
using LangLang.View;
using LangLang.View.CourseGUI;
using LangLang.View.ExamSlotGUI;
using LangLang.Core.Observer;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
Expand All @@ -16,20 +20,48 @@
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data;
using System.Reflection;
using System.Xml.Linq;
using System.Reflection.Emit;

namespace LangLang
{
/// <summary>
/// Interaction logic for TutorWindow.xaml
/// </summary>
public partial class TutorWindow : Window
public partial class TutorWindow : Window, IObserver
{
//for exam slots
public ObservableCollection<ExamSlotDTO> ExamSlots { get; set; }
public ExamSlotDTO SelectedExamSlot { get; set; }
private ExamSlotController examSlotsController { get; set; }
public Tutor tutor { get; set; }
private CourseController courseController;
public TutorWindow()
{
//tutor = t;
InitializeComponent();
DataContext = this;
ExamSlots = new ObservableCollection<ExamSlotDTO>();
examSlotsController = new ExamSlotController();

/*
ExamSlot es = new ExamSlot(1, 1, 10, DateTime.Now, 0);
ExamSlotDTO dto = new ExamSlotDTO(es);
ExamSlots.Add(dto);
examSlotsController.Add(es);
*/

examSlotsController.Subscribe(this);
Update();
}

public void Update()
{
ExamSlots.Clear();
foreach (ExamSlot exam in examSlotsController.GetAllExamSlots().Values)
ExamSlots.Add(new ExamSlotDTO(exam));
}

private void ExamSlotCreateWindowBtn_Click(object sender, RoutedEventArgs e)
Expand Down

0 comments on commit 8c17d3c

Please sign in to comment.