You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

66 lines
2.1 KiB

using CoviDok.Api.Objects;
using CoviDok.BLL.Sessions;
using CoviDok.BLL.User.Managers;
using CoviDok.Data.Model;
using CoviDok.Data.MySQL;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace CoviDok.Data.MySQL
{
public class ChildManager
{
private readonly IChildHandler handler = new MySqlChildHandler();
public async Task<PublicChild> GetChild(Session s, int id)
{
Child child = await handler.GetChild(id);
if (child == null) throw new KeyNotFoundException();
if (child.DoctorId == s.Id || child.ParentId == s.Id)
{
return child.ToPublic();
}
else {
throw new UnauthorizedAccessException();
}
}
public List<PublicChild> ChildrenOfParent(int parentId)
{
List<PublicChild> ret = new List<PublicChild>();
foreach (Child child in handler.GetChildren(parentId))
{
ret.Add(child.ToPublic());
}
return ret;
}
public async Task UpdateChild(Session s, int id, PublicChild newData)
{
if (id != newData.Id) throw new FormatException();
Child child = await handler.GetChild(id);
if (child == null) throw new KeyNotFoundException();
if (child.ParentId == s.Id || child.DoctorId == s.Id)
{
child.UpdateSelf(newData);
await handler.UpdateChild(id, child);
}
else
{
throw new UnauthorizedAccessException();
}
}
public async Task<int> AddChild(Session s, PublicChild newChild)
{
if (s.Id != newChild.ParentId) throw new UnauthorizedAccessException();
Child child = new Child();
child.UpdateSelf(newChild);
child.RegistrationDate = DateTime.Now;
return await handler.AddChild(child);
}
}
}