public dynamic CancelDoctorRequest(int requestId)
 {
     using (var db = new H2MDbContext())
     {
         var employeeRequest = db.EmployeeRequest.Where(a => a.Id == requestId).FirstOrDefault();
         if (employeeRequest == null)
         {
             return(new Response()
             {
                 Code = (int)HttpStatusCode.NotFound,
                 Data = "Invalid Request"
             });
         }
         else
         {
             db.EmployeeRequest.Remove(employeeRequest);
             string msg = "Cancelled Successfully!";
             db.SaveChanges();
             return(new Response()
             {
                 Code = (int)HttpStatusCode.OK, Data = new { msg }
             });
         }
     }
 }
        public static AuthObject CreateToken(H2MDbContext db, User user, string roleName)
        {
            var    yesterday = DateTime.Now.AddDays(-1);
            var    _token    = db.Tokens.Where(token => token.UserId == user.Id && token.ExpiryDate >= yesterday).FirstOrDefault();
            Tokens new_token = null;

            if (_token == null)
            {
                var exp = DateTime.Now.AddDays(30);
                new_token = new Tokens()
                {
                    CreationDate = DateTime.Now,
                    ExpiryDate   = exp,
                    Token        = Guid.NewGuid().ToString(),
                    RoleId       = user.RoleId,
                    UserId       = user.Id
                };
                db.Tokens.Add(new_token);
                db.SaveChanges();
            }
            else
            {
                new_token = _token;
            }
            var authObj = new AuthObject()
            {
                Role   = roleName,
                Token  = new_token.Token,
                UserId = user.Id
            };

            return(authObj);
        }
 public Response NewDemand([FromForm] int HospitalId, [FromForm] int count, [FromForm] string htmlPost, [FromForm] int SpecialityId)
 {
     using (var db = new H2MDbContext())
     {
         var request = new HostpitalRequest
         {
             Count        = count,
             Htmlpost     = htmlPost,
             HospitalId   = HospitalId,
             SpecialityId = SpecialityId,
             Enabled      = true
         };
         db.HostpitalRequest.Add(request);
         db.SaveChanges();
         return(new Response()
         {
             Code = (int)HttpStatusCode.OK,
             Data = "Demand added successfully!"
         });
     }
 }
 public Response Apply(int EmployeeId, int RequestId)
 {
     try
     {
         using (var db = new H2MDbContext())
         {
             var HasApplied = db.EmployeeRequest.Where(a => a.UserId == EmployeeId && a.RequestId == RequestId).FirstOrDefault() != null;
             if (HasApplied)
             {
                 return(new Response()
                 {
                     Code = (int)HttpStatusCode.AlreadyReported, Data = "You've applied before!"
                 });
             }
             else
             {
                 EmployeeRequest er = new EmployeeRequest()
                 {
                     UserId    = EmployeeId,
                     Time      = DateTime.Now,
                     RequestId = RequestId,
                     Status    = 0
                 };//0-> pending 1-> accepted -1-> rejected
                 db.EmployeeRequest.Add(er);
                 db.SaveChanges();
             }
         }
         return(new Response()
         {
             Code = (int)HttpStatusCode.OK, Data = "Applied Successfully"
         });
     }
     catch (Exception ex)
     {
         return(new Response()
         {
             Code = (int)HttpStatusCode.InternalServerError, Data = "Something wrong happend. Can you please try agian?"
         });
     }
 }
        public dynamic ApproveRejectApplications(int EmployeeRequestId, bool AcceptOrDecline)
        {
            using (var db = new H2MDbContext())
            {
                var employeeRequest = db.EmployeeRequest.Include(a => a.Request).Include(a => a.User).Where(a => a.Id == EmployeeRequestId).FirstOrDefault();
                if (employeeRequest == null)
                {
                    return(new Response()
                    {
                        Code = (int)HttpStatusCode.NotFound,
                        Data = "Invalid Request"
                    });
                }
                else
                {
                    var msg      = "";
                    var userInfo = db.User.Where(a => a.Id == employeeRequest.UserId).FirstOrDefault();

                    var request = db.HostpitalRequest.Where(a => a.Id == employeeRequest.RequestId).FirstOrDefault();

                    if (AcceptOrDecline)
                    {
                        employeeRequest.Status = 1;
                        msg = userInfo.Name + " accepted Successfully!";
                    }
                    else
                    {
                        employeeRequest.Status = -1;
                        msg = userInfo.Name + " has been rejected.";
                    }
                    db.SaveChanges();
                    return(new Response()
                    {
                        Code = (int)HttpStatusCode.OK, Data = new { userInfo.Name, userInfo.Email, msg }
                    });
                }
            }
        }
 public Response Signup([FromForm] string Email, [FromForm] string Password, [FromForm] string Gender, [FromForm] string Name, [FromForm] double?Longitude, [FromForm] double?Latitude, [FromForm] int?CountryId, [FromForm] int?CityId, [FromForm] int?RoleId, [FromForm] string SpecialitiesJson)
 {
     try
     {
         using (var db = new H2MDbContext())
         {
             var roleId  = (RoleId ?? 0);
             var newUser = new User()
             {
                 Email       = Email.Trim(),
                 Password    = Password,
                 RoleId      = roleId,
                 DateCreated = DateTime.Now,
                 Gender      = Gender,
                 Name        = Name.Trim(),
                 CountryId   = CountryId ?? -1,
                 CityId      = CityId ?? -1,
                 Longitude   = Longitude,
                 Latitude    = Latitude
             };
             db.SaveChanges();
             var authObject          = AuthHelper.CreateToken(db, newUser, newUser.Role.Name);
             List <Speciality> specs = null;
             if (!string.IsNullOrWhiteSpace(SpecialitiesJson))
             {
                 specs = JsonConvert.DeserializeObject <List <Speciality> >(SpecialitiesJson);
             }
             if (roleId == 1)
             {
                 var _hospital = new Hospital()
                 {
                     Id        = newUser.Id,
                     Latitude  = Latitude,
                     Longitude = Longitude
                 };
                 db.Hospital.Add(_hospital);
                 db.SaveChanges();
             }
             else if (roleId == 2)
             {
                 var _doctor = new Doctor()
                 {
                     DoctorId = newUser.Id.ToString()
                 };
                 specs.ForEach(s => _doctor.DoctorSpeciality.Add(new DoctorSpeciality()
                 {
                     Speciality = s,
                     DoctorId   = newUser.Id
                 }));
                 db.Doctor.Add(_doctor);
                 db.SaveChanges();
             }
             else if (roleId == 3)
             {
                 var _nurse = new Nurse()
                 {
                     NurseId = newUser.Id.ToString()
                 };
                 specs.ForEach(s => _nurse.NurseSpeciality.Add(new NurseSpeciality()
                 {
                     Speciality = s,
                     NurseId    = newUser.Id
                 }));
                 db.Nurse.Add(_nurse);
                 db.SaveChanges();
             }
             else
             {
                 InternalErrorObj.Data = "Invalid Job";
                 return(InternalErrorObj);
             }
             return(new Response()
             {
                 Code = (int)HttpStatusCode.OK,
                 Data = authObject
             });
         }
     }
     catch (Exception ex)
     {
         return(InternalErrorObj);
     }
 }