public ListResult <AvailabilityApiModel> List()
 {
     try
     {
         if (CheckUserPermission())
         {
             var avIds = Db.TeacherAvailabilities.Where(c => c.Teacher_UserId == CurrentUser.Id).Select(c => c.AvailabilityId).ToList();
             List <AvailabilityApiModel> avs = new List <AvailabilityApiModel>();
             foreach (var item in avIds)
             {
                 Availability         av         = Db.Availabilities.Find(item);
                 AvailabilityApiModel avApiModel = new AvailabilityApiModel()
                 {
                     Id        = av.Id,
                     StartTime = av.StartTime,
                     EndTime   = av.EndTime,
                     Date      = av.Date.ToString("MM/dd/yyyy"),
                     IsClosed  = av.IsClosed
                 };
                 avs.Add(avApiModel);
             }
             return(new ListResult <AvailabilityApiModel>()
             {
                 IsOk = true,
                 Message = new Message("Success, Availability Inserted Successfully", MessageType.Success),
                 Items = avs
             });
         }
         else
         {
             return(new ListResult <AvailabilityApiModel>()
             {
                 IsOk = true,
                 Message = new Message("Success, Availability Inserted Successfully", MessageType.Success),
                 Items = null
             });
         }
     }
     catch (Exception ex)
     {
         return(new ListResult <AvailabilityApiModel>()
         {
             IsOk = false,
             Message = new Message(ex.Message + " " + ex.InnerException?.Message, MessageType.Error),
             Items = null
         });
     }
 }
 public Result UpdateAvailability([FromBody] AvailabilityApiModel model)
 {
     try
     {
         if (CheckUserPermission())
         {
             if (ModelState.IsValid)
             {
                 var      currentDate = DateTime.ParseExact(model.Date, "MM/dd/yyyy", CultureInfo.InvariantCulture);
                 TimeSpan timeSpan    = new TimeSpan(0, 0, 0);
                 currentDate = currentDate.Date + timeSpan;
                 var av = Db.Availabilities.Include(c => c.TeacherAvailabilities)
                          .Where(cc => cc.Id == model.Id && cc.TeacherAvailabilities.Any(e => e.Teacher_UserId == CurrentUser.Id)).FirstOrDefault();
                 if (av == null)
                 {
                     return(new Result()
                     {
                         IsOk = true,
                         Message = new Message("Availability not found", MessageType.Error)
                     });
                 }
                 av.StartTime       = model.StartTime;
                 av.EndTime         = model.EndTime;
                 av.Date            = currentDate;
                 av.IsClosed        = model.IsClosed;
                 Db.Entry(av).State = EntityState.Modified;
                 Db.SaveChanges();
                 return(new Result()
                 {
                     IsOk = true,
                     Message = new Message("Availability Updated Successfully", MessageType.Success)
                 });
             }
             else
             {
                 List <string> errors = new List <string>();
                 foreach (var item in ModelState.Values)
                 {
                     foreach (var error in item.Errors)
                     {
                         errors.Add(error.ErrorMessage);
                     }
                 }
                 return(new Result()
                 {
                     IsOk = true,
                     Message = new Message(errors.ToString(), MessageType.Error),
                 });
             }
         }
         else
         {
             return(new Result()
             {
                 IsOk = true,
                 Message = new Message("You must logged in as teacher", MessageType.Error),
             });
         }
     }
     catch (Exception ex)
     {
         return(new Result()
         {
             IsOk = false,
             Message = new Message(ex.Message + " " + ex.InnerException?.Message, MessageType.Error),
         });
     }
 }