public static long ConnectRole(long id)
        {
            try
            {
                using (var ctx = new DAL.tutorDBEntities())
                {
                    if (ctx.Users.FirstOrDefault(x => x.id == id).role == "student")
                    {
                        var dbStudent = ctx.Students.FirstOrDefault(x => x.userId == id) ?? ctx.Students.Add(new DAL.Students());
                        dbStudent.userId = id;
                        ctx.SaveChanges();
                    }
                    if (ctx.Users.FirstOrDefault(x => x.id == id).role == "tutor")
                    {
                        var dbTutor = ctx.Tutors.FirstOrDefault(x => x.userId == id) ?? ctx.Tutors.Add(new DAL.Tutors());
                        dbTutor.userId = id;
                        ctx.SaveChanges();
                    }

                    return(id);
                }
            }
            catch (Exception ex)
            {
                throw;
                //return -1;
            }
        }
        public static long CreateUpdateLessonLevel(LessonLevelsDTO lessonLevels)
        {
            try
            {
                using (var ctx = new DAL.tutorDBEntities())
                {
                    var dbLessonLevel = ctx.LessonLevels.FirstOrDefault(x => x.id == lessonLevels.id) ?? ctx.LessonLevels.Add(new DAL.LessonLevels());

                    if (ctx.LessonLevels.Any(x => x.name == lessonLevels.name && x.id != dbLessonLevel.id))
                    {
                        throw new Exception($"Уровень образования: {lessonLevels.name} уже существует");
                    }


                    dbLessonLevel.name = lessonLevels.name;

                    ctx.SaveChanges();

                    return(dbLessonLevel.id);
                }
            }
            catch (Exception ex)
            {
                throw;
                //return -1;
            }
        }
        public static long UpdateTutor(TutorDTO tutor)
        {
            try
            {
                using (var ctx = new DAL.tutorDBEntities())
                {
                    var dbUser = ctx.Users.FirstOrDefault(x => x.id == tutor.id) ?? throw new Exception($"Пользователя не существует");


                    dbUser.name  = tutor.name;
                    dbUser.email = tutor.email;
                    //dbUser.emailVerified = student.emailVerified;
                    dbUser.birthDate = tutor.birthDate;


                    var dbTutor = ctx.Tutors.FirstOrDefault(x => x.userId == tutor.id) ?? throw new Exception($"Пользователя не существует");

                    dbTutor.Skype         = tutor.Skype;
                    dbTutor.info          = tutor.info;
                    dbTutor.adress        = tutor.adress;
                    dbTutor.avatarId      = tutor.avatarId;
                    dbTutor.education     = tutor.education;
                    dbTutor.qualification = tutor.qualification;
                    dbTutor.isApproved    = tutor.isApproved;
                    ctx.SaveChanges();

                    return(dbUser.id);
                }
            }
            catch (Exception ex)
            {
                throw;
                //return -1;
            }
        }
        public static long CreateUpdateSubjectTypes(SubjectTypesDTO subjectTypes)
        {
            try
            {
                using (var ctx = new DAL.tutorDBEntities())
                {
                    var dbSubjectType = ctx.SubjectTypes.FirstOrDefault(x => x.id == subjectTypes.id) ?? ctx.SubjectTypes.Add(new DAL.SubjectTypes());

                    if (ctx.SubjectTypes.Any(x => x.name == subjectTypes.name && x.id != dbSubjectType.id))
                    {
                        throw new Exception($"Группа предметов: {subjectTypes.name} уже существует");
                    }


                    dbSubjectType.name = subjectTypes.name;

                    ctx.SaveChanges();

                    return(dbSubjectType.id);
                }
            }
            catch (Exception ex)
            {
                throw;
                //return -1;
            }
        }
        public static double updateRating(long tutorId)
        {
            double rating;
            double sum          = 0;
            var    testimonials = BLL.Data.TutorData.displayTutorTestimonials(tutorId);

            if (testimonials.Count != 0)
            {
                foreach (var t in testimonials)
                {
                    sum += t.star;
                }
                rating = sum / testimonials.Count;
                try
                {
                    using (var ctx = new DAL.tutorDBEntities())
                    {
                        var dbTutor = ctx.Tutors.FirstOrDefault(x => x.userId == tutorId) ?? throw new Exception($"Заявки не существует");
                        dbTutor.rating = rating;
                        ctx.SaveChanges();
                        return(rating);
                    }
                }
                catch (Exception ex)
                {
                    throw;
                    //return -1;
                }
            }
            else
            {
                return(0);
            }
        }
        public static long SendRequestForAdmin(RequestForAdminDTO request)
        {
            try
            {
                using (var ctx = new DAL.tutorDBEntities())
                {
                    var dbRequest = ctx.RequestsForAdmins.FirstOrDefault(x => x.id == request.id) ?? ctx.RequestsForAdmins.Add(new DAL.RequestsForAdmins());

                    dbRequest.userId  = request.userId;
                    dbRequest.date    = request.date;
                    dbRequest.text    = request.text;
                    dbRequest.status  = request.status;
                    dbRequest.typeId  = request.typeId;
                    dbRequest.adminId = request.adminId;

                    ctx.SaveChanges();

                    return(dbRequest.id);
                }
            }
            catch (Exception ex)
            {
                throw;
                //return -1;
            }
        }
        public static long CreateUpdateUser(UserDTO user)
        {
            try
            {
                using (var ctx = new DAL.tutorDBEntities())
                {
                    var dbUser = ctx.Users.FirstOrDefault(x => x.id == user.id) ?? ctx.Users.Add(new DAL.Users());

                    if (ctx.Users.Any(x => x.email == user.email && x.id != dbUser.id))
                    {
                        return(-1);
                    }


                    dbUser.name          = user.name;
                    dbUser.email         = user.email;
                    dbUser.passwordHash  = user.passwordHash;
                    dbUser.passwordSalt  = user.passwordSalt;
                    dbUser.emailVerified = user.emailVerified;
                    dbUser.birthDate     = user.birthDate;
                    dbUser.regDate       = user.regDate;
                    dbUser.role          = user.role;

                    ctx.SaveChanges();

                    return(dbUser.id);
                }
            }
            catch (Exception ex)
            {
                throw;
                //return -1;
            }
        }
        public static long SendRequest(RequestDTO request)
        {
            try
            {
                using (var ctx = new DAL.tutorDBEntities())
                {
                    var dbRequest = ctx.Requests.FirstOrDefault(x => x.id == request.id) ?? ctx.Requests.Add(new DAL.Requests());

                    dbRequest.tutorId        = request.tutorId;
                    dbRequest.studentId      = request.studentId;
                    dbRequest.subjectId      = request.subjectId;
                    dbRequest.lessonsTypeId  = request.lessonTypeId;
                    dbRequest.additionalInfo = request.info;
                    dbRequest.status         = request.status;
                    dbRequest.date           = request.date;

                    ctx.SaveChanges();

                    return(dbRequest.id);
                }
            }
            catch (Exception ex)
            {
                throw;
                //return -1;
            }
        }
        public static long SendTestimonial(TestimonialDTO testimonial)
        {
            try
            {
                using (var ctx = new DAL.tutorDBEntities())
                {
                    var dbTestimonial = ctx.Testimonials.FirstOrDefault(x => x.id == testimonial.id) ?? ctx.Testimonials.Add(new DAL.Testimonials());

                    dbTestimonial.requestId = testimonial.requestId;
                    dbTestimonial.text      = testimonial.text;
                    dbTestimonial.date      = testimonial.date;
                    dbTestimonial.star      = testimonial.star;

                    ctx.SaveChanges();


                    return(dbTestimonial.id);
                }
            }
            catch (Exception ex)
            {
                throw;
                //return -1;
            }
        }
        public static long UpdateStudent(StudentDTO student)
        {
            try
            {
                using (var ctx = new DAL.tutorDBEntities())
                {
                    var dbUser = ctx.Users.FirstOrDefault(x => x.id == student.id) ?? throw new Exception($"Пользователя не существует");


                    dbUser.name  = student.name;
                    dbUser.email = student.email;
                    //dbUser.emailVerified = student.emailVerified;
                    dbUser.birthDate = student.birthDate;


                    var dbStudent = ctx.Students.FirstOrDefault(x => x.userId == student.id) ?? throw new Exception($"Пользователя не существует");

                    dbStudent.Skype          = student.Skype;
                    dbStudent.info           = student.info;
                    dbStudent.adress         = student.adress;
                    dbStudent.lessonsLevelId = student.lessonsLevelId;
                    dbStudent.avatarId       = student.avatarId;

                    ctx.SaveChanges();

                    return(dbUser.id);
                }
            }
            catch (Exception ex)
            {
                throw;
                //return -1;
            }
        }
        public static long CreateUpdateLessonTypes(LessonTypesDTO lessonTypes)
        {
            try
            {
                using (var ctx = new DAL.tutorDBEntities())
                {
                    var dbLessonType = ctx.LessonTypes.FirstOrDefault(x => x.id == lessonTypes.id) ?? ctx.LessonTypes.Add(new DAL.LessonTypes());

                    if (ctx.LessonTypes.Any(x => x.name == lessonTypes.name && x.id != dbLessonType.id))
                    {
                        throw new Exception($"Вариант проведения занятий: {lessonTypes.name} уже существует");
                    }


                    dbLessonType.name = lessonTypes.name;

                    ctx.SaveChanges();

                    return(dbLessonType.id);
                }
            }
            catch (Exception ex)
            {
                throw;
                //return -1;
            }
        }
        public static long AddImage(long UserId, ImageWrapper ImageData, bool isDocument)
        {
            using (var ctx = new DAL.tutorDBEntities())
            {
                var img = ctx.Images.Add(new DAL.Images
                {
                    userId       = UserId,
                    photoContent = ImageData.Content,
                    photoMime    = ImageData.Mime,
                    isDocument   = isDocument
                });


                ctx.SaveChanges();

                return(img.id);
            }
        }
Exemple #13
0
 public static string processRequest(long requestId)
 {
     try
     {
         using (var ctx = new DAL.tutorDBEntities())
         {
             var dbAdminRequest = ctx.RequestsForAdmins.FirstOrDefault(x => x.id == requestId) ?? throw new Exception($"Заявки не существует");
             dbAdminRequest.status = "выполнена";
             ctx.SaveChanges();
             return(dbAdminRequest.status);
         }
     }
     catch (Exception ex)
     {
         throw;
         //return -1;
     }
 }
        public static long ChangeRequestStatus(long requestId, string status)
        {
            try
            {
                using (var ctx = new DAL.tutorDBEntities())
                {
                    var dbRequest = ctx.Requests.FirstOrDefault(x => x.id == requestId) ?? throw new Exception($"Заявки не существует");
                    dbRequest.status = status;

                    ctx.SaveChanges();

                    return(dbRequest.id);
                }
            }
            catch (Exception ex)
            {
                throw;
                //return -1;
            }
        }
        public static long SetTutorsSubjects(TutorSubjectsDTO subject)
        {
            try
            {
                using (var ctx = new DAL.tutorDBEntities())
                {
                    var dbTutorSubject = ctx.TutorSubjects.FirstOrDefault(x => x.subjectId == subject.subjectId && x.tutorId == subject.tutorId) ?? ctx.TutorSubjects.Add(new DAL.TutorSubjects());


                    dbTutorSubject.tutorId   = subject.tutorId;
                    dbTutorSubject.subjectId = subject.subjectId;

                    ctx.SaveChanges();

                    return(dbTutorSubject.subjectId);
                }
            }
            catch (Exception ex)
            {
                return(-1);
            }
        }
        public static long SetTutorsLessonTypes(TutorLessonTypeDTO type)
        {
            try
            {
                using (var ctx = new DAL.tutorDBEntities())
                {
                    var dbTutorLessonType = ctx.TutorLessonType.FirstOrDefault(x => x.lessonTypeId == type.lessontypeId && x.tutorId == type.tutorId) ?? ctx.TutorLessonType.Add(new DAL.TutorLessonType());


                    dbTutorLessonType.tutorId      = type.tutorId;
                    dbTutorLessonType.lessonTypeId = type.lessontypeId;
                    dbTutorLessonType.cost         = type.cost;

                    ctx.SaveChanges();

                    return(dbTutorLessonType.lessonTypeId);
                }
            }
            catch (Exception ex)
            {
                return(-1);
            }
        }
        public static bool DeleteTutorsLessonTypes(TutorLessonTypeDTO type)
        {
            try
            {
                using (var ctx = new DAL.tutorDBEntities())
                {
                    var dbTutorLessonType = ctx.TutorLessonType.SingleOrDefault(x => x.lessonTypeId == type.lessontypeId && x.tutorId == type.tutorId);

                    if (dbTutorLessonType != null)
                    {
                        ctx.TutorLessonType.Remove(dbTutorLessonType);
                    }

                    ctx.SaveChanges();

                    return(true);
                }
            }
            catch (Exception ex)
            {
                return(false);
            }
        }
        public static long SetTutorsLessonLevels(TutorLessonLevelDTO level)
        {
            try
            {
                using (var ctx = new DAL.tutorDBEntities())
                {
                    var dbTutorLessonLevel = ctx.TutorLessonLevel.FirstOrDefault(x => x.lessonLevelId == level.lessonlevelId && x.tutorId == level.tutorId) ?? ctx.TutorLessonLevel.Add(new DAL.TutorLessonLevel());


                    dbTutorLessonLevel.tutorId       = level.tutorId;
                    dbTutorLessonLevel.lessonLevelId = level.lessonlevelId;
                    dbTutorLessonLevel.addCost       = 0;

                    ctx.SaveChanges();

                    return(dbTutorLessonLevel.lessonLevelId);
                }
            }
            catch (Exception ex)
            {
                return(-1);
            }
        }
        public static bool DeleteTutorsLessonLevels(TutorLessonLevelDTO level)
        {
            try
            {
                using (var ctx = new DAL.tutorDBEntities())
                {
                    var dbTutorLessonLevel = ctx.TutorLessonLevel.SingleOrDefault(x => x.lessonLevelId == level.lessonlevelId && x.tutorId == level.tutorId);

                    if (dbTutorLessonLevel != null)
                    {
                        ctx.TutorLessonLevel.Remove(dbTutorLessonLevel);
                    }

                    ctx.SaveChanges();

                    return(true);
                }
            }
            catch (Exception ex)
            {
                return(false);
            }
        }
        public static bool DeleteTutorsSubjects(TutorSubjectsDTO subject)
        {
            try
            {
                using (var ctx = new DAL.tutorDBEntities())
                {
                    var dbTutorSubject = ctx.TutorSubjects.SingleOrDefault(x => x.subjectId == subject.subjectId && x.tutorId == subject.tutorId);

                    if (dbTutorSubject != null)
                    {
                        ctx.TutorSubjects.Remove(dbTutorSubject);
                    }

                    ctx.SaveChanges();

                    return(true);
                }
            }
            catch (Exception ex)
            {
                return(false);
            }
        }