public Course CreateCourse(int userId, NewCourseModel newCourseModel)
        {
            Course course = new Course()
            {
                UserId = userId,
                TermId = newCourseModel.TermId,
                Year = newCourseModel.Year,
                CourseStateId = 1
            };

            db.Courses.Add(course);

            var rand = new Random((int)new DateTime().Ticks);

            for (int i = 0; i < newCourseModel.WorksCount; i++)
            {
                Work work = new Work()
                {
                    Name = RandomString(9),
                    ShortName = RandomString(3),
                    WorkType = db.WorkTypes.Find(1),
                    WorkControlType = db.WorkControlTypes.Find(1),
                    Course = course
                };

                db.Works.Add(work);

                //create stages
                //int stagesCount = rand.Next(5) + 2;
                int stagesCount = 1;

                for (int s = 0; s < stagesCount; s++)
                {
                    WorkStage ws = new WorkStage()
                    {
                        Name = RandomString(10),
                        Work = work
                    };

                    db.WorkStages.Add(ws);

                    //bool addSubStages = rand.Next(10) > 7;
                    bool addSubStages = false;

                    if(addSubStages)
                    {
                        int substages = rand.Next(3) + 2;

                        for(int j = 0; j < substages; j++)
                        {
                            WorkStage subStage = new WorkStage()
                            {
                                Name = RandomString(10),
                                Work = work,
                                ParentStage = ws
                            };

                            db.WorkStages.Add(subStage);

                        }
                    }
                }

                course.Works.Add(work);
            }

            course.Packs = new List<Pack>();

            if (newCourseModel.LessonPackGroupIds != null)
            {
                Pack pack = new Pack()
                {
                    Course = course,
                    PackType = db.PackTypes.Find(1),
                    Groups = db.Groups.Where(g => newCourseModel.LessonPackGroupIds.Contains(g.Id)).ToList()
                };
                course.Packs.Add(pack);
            }

            if (newCourseModel.LabsGroupIds != null)
            {
                foreach (var groupId in newCourseModel.LabsGroupIds)
                {
                    Pack pack = new Pack()
                    {
                        Course = course,
                        PackType = db.PackTypes.Find(2),
                        Groups = db.Groups.Where(g => g.Id == groupId).ToList()
                    };
                    course.Packs.Add(pack);
                }
            }

            db.SaveChanges();

            return course;
        }
        // POST api/<controller>
        public HttpResponseMessage Post([FromBody]NewCourseModel courseModel)
        {
            var course = courseModel.Course;
            var user = GetCurrentUser();

            //check existed course with same Year and Term
            var existedCourse = db.Courses.FirstOrDefault(c => c.TermId == course.Term.Id && c.Year == course.Year && c.UserId == user.Id);

            if(existedCourse != null)
            {
                HttpError err = new HttpError("У вас уже есть курс данного семестра на выбранный год");
                return Request.CreateResponse(HttpStatusCode.Forbidden, err);
            }

            var term = db.Terms.Find(course.Term.Id);

            var newCourse = new Course()
            {
                Term = term,
                Year = course.Year,
                User = user,
                CourseState = db.CourseStates.Find(1),
                Packs = new List<Pack>()
            };

            foreach (var pack in course.Packs)
            {
                var packGroupIds = pack.Groups.Select(g => g.Id);

                var packType = db.PackTypes.Find(pack.PackType.Id);
                var groups = db.Groups.Where(g => packGroupIds.Contains(g.Id)).ToList();

                newCourse.Packs.Add(new Pack()
                {
                    PackType = packType,
                    Groups = groups
                });
            }

            List<Work> prevCourseWorks = new List<Work>();

            using(HomeServerContext context = new HomeServerContext())
            {
                var prevCourse = context.Courses.Find(courseModel.PrevId);

                if (prevCourse != null)
                {
                    foreach (var work in prevCourse.Works)
                    {
                        var workCopy = new Work()
                        {
                            Name = work.Name,
                            ShortName = work.ShortName,
                            HasThemes = work.HasThemes,
                            WorkTypeId = work.WorkTypeId,
                            WorkControlTypeId = work.WorkControlTypeId,
                            WorkStages = new List<WorkStage>()
                        };

                        prevCourseWorks.Add(workCopy);

                        var tempStages = context.WorkStages.Where(cws => cws.WorkId == work.Id
                            && cws.ParentWorkStageId == null).ToList();

                        var stages = tempStages.Select(ws => new WorkStage() { Id = ws.Id, Name = ws.Name, Optional = ws.Optional})
                            .ToList();

                        for (int i = 0; i < stages.Count; i++)
                        {
                            var workStage = stages[i];

                            var tempSubstages = context.WorkStages
                                .Where(cws => cws.ParentWorkStageId == workStage.Id).ToList();

                            workStage.SubStages = tempSubstages
                                .Select(ws => new WorkStage() { Id = ws.Id, Name = ws.Name, Optional = ws.Optional })
                                .ToList();
                        }

                        workCopy.WorkStages = stages;
                    }
                }
            }

            newCourse = db.Courses.Add(newCourse);
            db.SaveChanges();

            //copy works from previous course

            //if (prevCourse != null)
            //{
                db.SaveChanges();

                foreach (var work in prevCourseWorks)
                {
                    var newWork = new Work()
                    {
                        Course = newCourse,
                        Name = work.Name,
                        ShortName = work.ShortName,
                        HasThemes = work.HasThemes,
                        WorkTypeId = work.WorkTypeId,
                        WorkControlTypeId = work.WorkControlTypeId
                    };

                    db.SaveChanges();
                    newWork = db.Works.Add(newWork);
                    db.SaveChanges();

                    foreach(var workStage in work.WorkStages)
                    {
                        var newWorkStage = new WorkStage()
                        {
                            Name = workStage.Name,
                            Optional = workStage.Optional,
                            Work = newWork
                        };

                        newWorkStage = db.WorkStages.Add(newWorkStage);
                        db.SaveChanges();

                        if (workStage.SubStages != null)
                        {
                            foreach (var subStage in workStage.SubStages)
                            {
                                var newSubStage = new WorkStage()
                                {
                                    Name = subStage.Name,
                                    Optional = subStage.Optional,
                                    ParentStage = newWorkStage,
                                    Work = newWork
                                };

                                db.WorkStages.Add(newSubStage);
                                db.SaveChanges();
                            }
                        }
                    }
                }
            //}

            db.SaveChanges();
            return Request.CreateResponse(HttpStatusCode.OK);
        }