Exemple #1
0
        public HttpResponseMessage PutSchool(int id, School schoolEntity)
        {
            if (id <= 0)
            {
                var errResponse = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest,
                                                                   "The id of the school must be positive");
                return(errResponse);
            }

            try
            {
                var entity = this.schoolRepository.Update(id, schoolEntity);

                var response = this.Request.CreateResponse(
                    HttpStatusCode.OK, SchoolModel.CreateFromSchoolEntity(entity));
                response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = entity.SchoolId }));

                return(response);
            }
            catch (DbUpdateConcurrencyException)
            {
                var errResponse = this.Request.CreateErrorResponse(HttpStatusCode.InternalServerError,
                                                                   "The school could not be updated because of a concurrency problem");

                return(errResponse);
            }
            catch (NullReferenceException)
            {
                var errResponse = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest,
                                                                   string.Format("A School entity with id={0} does not exist in the Database", id));

                return(errResponse);
            }
        }
Exemple #2
0
        public HttpResponseMessage PostSchool(School schoolEntity)
        {
            if (schoolEntity == null || schoolEntity.Name == null || schoolEntity.Location == null)
            {
                var errResponse =
                    this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, "The school is invalid");

                return(errResponse);
            }

            try
            {
                var entity = this.schoolRepository.Add(schoolEntity);

                var response = this.Request.CreateResponse(
                    HttpStatusCode.Created, SchoolModel.CreateFromSchoolEntity(schoolEntity));
                response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = entity.SchoolId }));

                return(response);
            }
            catch (DbUpdateConcurrencyException)
            {
                var errResponse = this.Request.CreateErrorResponse(HttpStatusCode.InternalServerError,
                                                                   "The school could not be added because of a concurrency problem");
                return(errResponse);
            }
        }
Exemple #3
0
        public IEnumerable <SchoolModel> GetAll()
        {
            var entities = this.schoolRepository.GetAll();

            List <SchoolModel> models = new List <SchoolModel>();

            foreach (var entity in entities)
            {
                models.Add(SchoolModel.CreateFromSchoolEntity(entity));
            }

            return(models);
        }
Exemple #4
0
        public SchoolModel Get(int id)
        {
            if (id <= 0)
            {
                var errResponse = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest,
                                                                   "The id of the school must be positive");

                throw new HttpResponseException(errResponse);
            }

            var entity = this.schoolRepository.GetById(id);

            if (entity != null)
            {
                SchoolModel model = SchoolModel.CreateFromSchoolEntity(entity);
                return(model);
            }
            else
            {
                return(null);
            }
        }