示例#1
0
        public HttpResponseMessage PutMark(int id, Mark markEntity)
        {
            if (id <= 0)
            {
                var errResponse = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest,
                                                                   "The id of the mark must be positive");
                return(errResponse);
            }

            try
            {
                var entity = this.markRepository.Update(id, markEntity);

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

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

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

                return(errResponse);
            }
        }
示例#2
0
        public HttpResponseMessage PostMark(Mark markEntity)
        {
            if (markEntity == null || markEntity.Subject == null || markEntity.Value == null)
            {
                var errResponse =
                    this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, "The mark is invalid");

                return(errResponse);
            }

            try
            {
                var entity = this.markRepository.Add(markEntity);

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

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

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

            foreach (var entity in entities)
            {
                models.Add(MarkModel.CreateFromMarkEntity(entity));
            }

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

                throw new HttpResponseException(errResponse);
            }

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

            if (entity != null)
            {
                MarkModel model = MarkModel.CreateFromMarkEntity(entity);
                return(model);
            }
            else
            {
                return(null);
            }
        }