コード例 #1
0
ファイル: TeacherController.cs プロジェクト: tanmyblog/webapi
        public IHttpActionResult Create(CreateTeacher model)
        {
            IHttpActionResult httpActionResult;
            ErrorsModel       errors = new ErrorsModel();

            if (string.IsNullOrEmpty(model.Code))
            {
                errors.Add("Mã giảng viên là trường bắt buộc");
            }

            if (string.IsNullOrEmpty(model.Name))
            {
                errors.Add("Tên giảng viên là trường bắt buộc");
            }

            if (errors.Errors.Count == 0)
            {
                Teachers t = new Teachers();
                t.Code = model.Code;
                t.Name = model.Name;
                t      = _db.Teachers.Add(t);

                this._db.SaveChanges();

                TeacherModel viewModel = new TeacherModel(t);

                httpActionResult = Ok(viewModel);
            }
            else
            {
                httpActionResult = new ErrorActionResult(Request, System.Net.HttpStatusCode.BadRequest, errors);
            }

            return(httpActionResult);
        }
コード例 #2
0
        public IHttpActionResult Update(UpdateRestaurant model, string id)
        {
            IHttpActionResult httpActionResult;
            ErrorsModel       errors = new ErrorsModel();

            Restaurants gv = this._db.Restaurants.FirstOrDefault(x => x.id == id);

            if (gv == null)
            {
                errors.Add("Nhà hàng này không tồn tại hoặc rỗng");

                httpActionResult = new ErrorActionResult(Request, System.Net.HttpStatusCode.NotFound, errors);
            }
            else
            {
                gv.name    = model.name ?? model.name;
                gv.address = model.address ?? model.address;
                gv.type    = model.type ?? model.type;

                this._db.Entry(gv).State = System.Data.Entity.EntityState.Modified;

                this._db.SaveChanges();

                httpActionResult = Ok(new RestaurantModel(gv));
            }

            return(httpActionResult);
        }
コード例 #3
0
        public IHttpActionResult Create(CreateRestaurant model)
        {
            IHttpActionResult httpActionResult;
            ErrorsModel       errors = new ErrorsModel();

            if (string.IsNullOrEmpty(model.name))
            {
                errors.Add("Tên nhà hàng là trường bắt buộc");
            }

            if (errors.Errors.Count == 0)
            {
                Restaurants t = new Restaurants();
                t.id      = model.id;
                t.name    = model.name;
                t.address = model.address;
                t.type    = model.type;

                t = _db.Restaurants.Add(t);

                this._db.SaveChanges();

                RestaurantModel viewModel = new RestaurantModel(t);

                httpActionResult = Ok(viewModel);
            }
            else
            {
                httpActionResult = new ErrorActionResult(Request, System.Net.HttpStatusCode.BadRequest, errors);
            }

            return(httpActionResult);
        }
コード例 #4
0
ファイル: StudentController.cs プロジェクト: tanmyblog/webapi
        public IHttpActionResult Update(UpdateStudentModel model)
        {
            IHttpActionResult httpActionResult;
            ErrorsModel       errors = new ErrorsModel();

            Students sv = this._db.Students.FirstOrDefault(x => x.Id == model.Id);

            if (sv == null)
            {
                errors.Add("Không tìm thấy mã sinh viên này");

                httpActionResult = new ErrorActionResult(Request, System.Net.HttpStatusCode.NotFound, errors);
            }
            else
            {
                sv.Code     = model.Code ?? model.Code;
                sv.Name     = model.Name ?? model.Name;
                sv.BirthDay = model.BirthDay ?? model.BirthDay;
                sv.Address  = model.Address ?? model.Address;

                this._db.Entry(sv).State = System.Data.Entity.EntityState.Modified;

                this._db.SaveChanges();

                httpActionResult = Ok(new StudentModel(sv));
            }

            return(httpActionResult);
        }
コード例 #5
0
ファイル: TeacherController.cs プロジェクト: tanmyblog/webapi
        public IHttpActionResult Update(UpdateTeacher model)
        {
            IHttpActionResult httpActionResult;
            ErrorsModel       errors = new ErrorsModel();

            Teachers gv = this._db.Teachers.FirstOrDefault(x => x.Id == model.Id);

            if (gv == null)
            {
                errors.Add("Mã giảng viên không tồn tại hoặc rỗng");

                httpActionResult = new ErrorActionResult(Request, System.Net.HttpStatusCode.NotFound, errors);
            }
            else
            {
                gv.Code = model.Code ?? model.Code;
                gv.Name = model.Name ?? model.Name;

                this._db.Entry(gv).State = System.Data.Entity.EntityState.Modified;

                this._db.SaveChanges();

                httpActionResult = Ok(new TeacherModel(gv));
            }

            return(httpActionResult);
        }
コード例 #6
0
ファイル: ClassController.cs プロジェクト: tanmyblog/webapi
        public IHttpActionResult Update(UpdateClass model)
        {
            IHttpActionResult httpActionResult;
            ErrorsModel       errors = new ErrorsModel();

            Classes l = this._db.Classes.FirstOrDefault(x => x.Id == model.Id);

            if (l == null)
            {
                errors.Add("Id lớp không tồn tại");

                httpActionResult = new ErrorActionResult(Request, System.Net.HttpStatusCode.NotFound, errors);
            }
            else
            {
                l.Code            = model.Code ?? model.Code;
                l.Name            = model.Name ?? model.Name;
                l.HomeroomTeacher = _db.Teachers.FirstOrDefault(x => x.Id == model.HomeroomTeacher_Id) ?? _db.Teachers.FirstOrDefault(x => x.Id == model.HomeroomTeacher_Id);
                l.Teacher         = _db.Teachers.FirstOrDefault(x => x.Id == model.Teacher_Id) ?? _db.Teachers.FirstOrDefault(x => x.Id == model.Teacher_Id);

                this._db.Entry(l).State = System.Data.Entity.EntityState.Modified;

                this._db.SaveChanges();

                httpActionResult = Ok(new ClassModel(l));
            }

            return(httpActionResult);
        }
コード例 #7
0
ファイル: ClassController.cs プロジェクト: tanmyblog/webapi
        public IHttpActionResult Create(CreateClass model)
        {
            IHttpActionResult httpActionResult;
            ErrorsModel       errors = new ErrorsModel();

            if (string.IsNullOrEmpty(model.Code))
            {
                errors.Add("Mã lớp là trường bắt buộc");
            }

            if (string.IsNullOrEmpty(model.Name))
            {
                errors.Add("Tên lớp là trường bắt buộc");
            }

            if (_db.Teachers.FirstOrDefault(m => m.Id == model.HomeroomTeacher_Id) == null)
            {
                errors.Add("Id giảng viên chu nhiệm không tồn tại");
            }

            if (errors.Errors.Count == 0)
            {
                Classes l = new Classes();
                l.Code            = model.Code;
                l.Name            = model.Name;
                l.HomeroomTeacher = _db.Teachers.FirstOrDefault(x => x.Id == model.HomeroomTeacher_Id);
                l.Teacher         = _db.Teachers.FirstOrDefault(x => x.Id == model.Teacher_Id);
                l = _db.Classes.Add(l);

                this._db.SaveChanges();

                ClassModel viewModel = new ClassModel(l);

                httpActionResult = Ok(viewModel);
            }
            else
            {
                httpActionResult = new ErrorActionResult(Request, System.Net.HttpStatusCode.BadRequest, errors);
            }

            return(httpActionResult);
        }
コード例 #8
0
ファイル: StudentController.cs プロジェクト: tanmyblog/webapi
        public IHttpActionResult Create(CreateStudentMdoel model)
        {
            IHttpActionResult httpActionResult;
            ErrorsModel       errors = new ErrorsModel();

            if (string.IsNullOrEmpty(model.Code))
            {
                errors.Add("Mã sinh viên là trường bắt buộc");
            }

            if (string.IsNullOrEmpty(model.Name))
            {
                errors.Add("Tên sinh viên là trường bắt buộc");
            }

            if (_db.Classes.FirstOrDefault(m => m.Id == model.Class_Id) == null)
            {
                errors.Add("Không tìm thấy lớp này");
            }

            if (errors.Errors.Count == 0)
            {
                Students sv = new Students();
                sv.Code     = model.Code;
                sv.Name     = model.Name;
                sv.BirthDay = model.BirthDay;
                sv.Address  = model.Address;
                sv          = _db.Students.Add(sv);

                this._db.SaveChanges();

                StudentModel viewModel = new StudentModel(sv);

                httpActionResult = Ok();
            }
            else
            {
                httpActionResult = new ErrorActionResult(Request, System.Net.HttpStatusCode.BadRequest, errors);
            }
            return(httpActionResult);
        }
コード例 #9
0
        public IHttpActionResult GetById(string id)
        {
            IHttpActionResult httpActionResult;
            var gv = _db.Restaurants.FirstOrDefault(x => x.id == id);

            if (gv == null)
            {
                ErrorsModel errors = new ErrorsModel();
                errors.Add("Không tìm thấy nhà hàng này");

                httpActionResult = new ErrorActionResult(Request, System.Net.HttpStatusCode.NotFound, errors);
            }
            else
            {
                httpActionResult = Ok(new RestaurantModel(gv));
            }

            return(httpActionResult);
        }
コード例 #10
0
ファイル: TeacherController.cs プロジェクト: tanmyblog/webapi
        public IHttpActionResult GetById(int id)
        {
            IHttpActionResult httpActionResult;
            var gv = _db.Teachers.FirstOrDefault(x => x.Id == id);

            if (gv == null)
            {
                ErrorsModel errors = new ErrorsModel();
                errors.Add("Không tìm thấy giảng viên này");

                httpActionResult = new ErrorActionResult(Request, System.Net.HttpStatusCode.NotFound, errors);
            }
            else
            {
                httpActionResult = Ok(new TeacherModel(gv));
            }

            return(httpActionResult);
        }
コード例 #11
0
ファイル: StudentController.cs プロジェクト: tanmyblog/webapi
        public IHttpActionResult GetById(int id)
        {
            IHttpActionResult httpActionResult;
            var sv = _db.Students.FirstOrDefault(x => x.Id == id);

            if (sv == null)
            {
                ErrorsModel errors = new ErrorsModel();
                errors.Add("Không tìm thấy mã sinh viên này");

                httpActionResult = new ErrorActionResult(Request, System.Net.HttpStatusCode.NotFound, errors);
            }
            else
            {
                httpActionResult = Ok(new StudentModel(sv));
            }

            return(httpActionResult);
        }
コード例 #12
0
ファイル: ClassController.cs プロジェクト: tanmyblog/webapi
        public IHttpActionResult GetById(int id)
        {
            IHttpActionResult httpActionResult;
            var l = _db.Classes.FirstOrDefault(x => x.Id == id);

            if (l == null)
            {
                ErrorsModel errors = new ErrorsModel();
                errors.Add("Không tìm thấy lớp này");

                httpActionResult = new ErrorActionResult(Request, System.Net.HttpStatusCode.NotFound, errors);
            }
            else
            {
                httpActionResult = Ok(new ClassModel(l));
            }

            return(httpActionResult);
        }
コード例 #13
0
        public IHttpActionResult Delete(string id)
        {
            IHttpActionResult httpActionResult;
            ErrorsModel       errors = new ErrorsModel();

            Restaurants gv = this._db.Restaurants.FirstOrDefault(x => x.id == id);

            if (gv == null)
            {
                errors.Add("Nhà hàng này không tồn tại hoặc rỗng");

                httpActionResult = new ErrorActionResult(Request, System.Net.HttpStatusCode.NotFound, errors);
            }
            else
            {
                this._db.Entry(gv).State = System.Data.Entity.EntityState.Deleted;

                this._db.SaveChanges();

                httpActionResult = Ok(new RestaurantModel(gv));
            }

            return(httpActionResult);
        }