Ejemplo n.º 1
0
        public static void Main()
        {
            //Create and initialize the database

            Database.SetInitializer(new MigrateDatabaseToLatestVersion
                <SchoolContext, School.Data.Migrations.Configuration>());

            var context = new SchoolContext();
            using (context)
            {
                var school = new TownSchool { Name = "School", Location = "Town" };
                var firstStudent = new Student { FirstName = "Ivan", LastName = "Ivanov", Age = 9, Grade = 4, TownSchool = school };
                var secondStudent = new Student { FirstName = "Peter", LastName = "Petrov", Age = 15, Grade = 9, TownSchool = school };
                var firstMark = new Mark { Subject = "Math", Value = 5, Student = firstStudent };
                var secondMark = new Mark { Subject = "History", Value = 4, Student = secondStudent };
                var thirdMark = new Mark { Subject = "IT", Value = 6, Student = secondStudent };

                context.Marks.Add(firstMark);
                context.Marks.Add(secondMark);
                context.Marks.Add(thirdMark);
                context.Students.Add(firstStudent);
                context.Students.Add(secondStudent);
                context.TownSchools.Add(school);
                context.SaveChanges();
            }
        }
Ejemplo n.º 2
0
        public static MarkModel Convert(Mark mark)
        {
            MarkModel model = new MarkModel
            {
                MarkId = mark.MarkId,
                Subject = mark.Subject,
                Value = mark.Value,
                StudentId = mark.StudentId
            };

            return model;
        }
Ejemplo n.º 3
0
        public static MarkFullModel Convert(Mark mark)
        {
            MarkFullModel model = new MarkFullModel
            {
                MarkId = mark.MarkId,
                Subject = mark.Subject,
                Value = mark.Value,
                Student = new StudentModel
                {
                    StudentId = mark.Student.StudentId,
                    FirstName = mark.Student.FirstName,
                    LastName = mark.Student.LastName,
                    Age = mark.Student.Age,
                    Grade = mark.Student.Grade,
                    MarksCount = mark.Student.Marks.Count,
                    TownSchoolId = mark.Student.TownSchoolId
                }
            };

            return model;
        }
Ejemplo n.º 4
0
        // POST api/artists
        //[HttpPost]
        public HttpResponseMessage Post(Mark model)
        {
            if (model.Subject == null)
            {
                var errResponse = this.Request.CreateErrorResponse(
                    HttpStatusCode.BadRequest, "Mark Subject could not be null");
                throw new HttpResponseException(errResponse);
            }

            if (model.Value <= 0 || model.StudentId <= 0)
            {
                var errResponse = this.Request.CreateErrorResponse(
                    HttpStatusCode.BadRequest, "Mark Value and StudentId should be positive numbers");
                throw new HttpResponseException(errResponse);
            }

            var entity = this.markRepository.Add(model);
            var response =
                Request.CreateResponse(HttpStatusCode.Created, entity);
            response.Headers.Location = new Uri(Url.Link("DefaultApi",
                new { id = entity.MarkId }));

            return response;
        }
Ejemplo n.º 5
0
        // PUT api/artists/5
        //[HttpPut]
        public HttpResponseMessage Put(int id, Mark model)
        {
            if (model.Subject == null)
            {
                var errResponse = this.Request.CreateErrorResponse(
                    HttpStatusCode.BadRequest, "Mark Subject could not be null");
                throw new HttpResponseException(errResponse);
            }

            if (model.Value <= 0 || model.StudentId <= 0)
            {
                var errResponse = this.Request.CreateErrorResponse(
                    HttpStatusCode.BadRequest, "Mark Value and StudentId should be positive numbers");
                throw new HttpResponseException(errResponse);
            }

            var entity = this.markRepository.Get(id);

            if (entity == null)
            {
                var errResponse = this.Request.CreateErrorResponse(
                    HttpStatusCode.BadRequest, string.Format("There is no element with id {0}", id));
                throw new HttpResponseException(errResponse);
            }

            entity = this.markRepository.Update(id, model);
            var response =
                Request.CreateResponse(HttpStatusCode.OK, entity);
            response.Headers.Location = new Uri(Url.Link("DefaultApi",
                new { id = entity.MarkId }));

            return response;
        }
        public void UpdeteStudentMarksTest()
        {
            var school = new TownSchool { Name = "School" };
            var student = new Student { FirstName = "Sam", LastName = "Chen", Age = 9, Grade = 4, TownSchool = school };
            var mark = new Mark { Subject = "Math", Value = 5, Student = student };
            dbContext.Set<TownSchool>().Add(school);
            dbContext.Set<Student>().Add(student);
            dbContext.Set<Mark>().Add(mark);
            dbContext.SaveChanges();

            var studentId = student.StudentId;
            var schoolId = student.TownSchoolId;
            student.Marks.First().Subject = "IT";
            var updatedStudent = this.studentsRepository.Update(studentId, student);
            Assert.IsNotNull(updatedStudent);
            Assert.AreEqual(1, updatedStudent.Marks.Count);
            Assert.AreEqual("IT", updatedStudent.Marks.First().Subject);
            Assert.AreEqual(5, updatedStudent.Marks.First().Value);
        }