Пример #1
0
        /// <summary>
        /// Add new
        /// </summary>
        /// <param name="newSubject">New SubjectAdd object</param>
        /// <returns>SubjectFull object</returns>
        public SubjectFull AddNew(SubjectAdd newSubject)
        {
            // Must validate the incoming ProgramId...
            var p = ds.Programs.Find(newSubject.ProgramId);
            if (p == null) return null;

            // Add the Subject object
            var s = ds.Subjects.Add(Mapper.Map<Models.Subject>(newSubject));
            // Configure the Program object association
            s.Program = p;
            ds.SaveChanges();

            return Mapper.Map<SubjectFull>(s);

            // Alternative, return a SubjectWithProgram object
        }
Пример #2
0
        /// <summary>
        /// Add new
        /// </summary>
        /// <param name="newSubject">New SubjectAdd object</param>
        /// <returns>SubjectFull object</returns>
        public SubjectFull AddNew(SubjectAdd newSubject)
        {
            // Must validate the incoming ProgramId...
            var p = ds.Programs.Find(newSubject.ProgramId);

            if (p == null)
            {
                return(null);
            }

            // Add the Subject object
            var s = ds.Subjects.Add(Mapper.Map <Models.Subject>(newSubject));

            // Configure the Program object association
            s.Program = p;
            ds.SaveChanges();

            return(Mapper.Map <SubjectFull>(s));

            // Alternative, return a SubjectWithProgram object
        }
        // POST api/subjects
        // Notice that we're using the SubjectAdd type in this method
        // We need a Program Id in the message body
        /// <summary>
        /// Add a new Subject object to the collection
        /// </summary>
        /// <param name="newSubject">Fully-configured Subject object, with its program identifier</param>
        /// <returns>Fully-configured new Subject object</returns>
        public HttpResponseMessage Post(SubjectAdd newSubject)
        {
            if (ModelState.IsValid)
            {
                // Add the new program
                var s = r.AddNew(newSubject);

                // Make sure that we can continue
                if (s == null)
                {
                    // We probably need a better status code...
                    return Request.CreateResponse(HttpStatusCode.UnsupportedMediaType);
                }
                else
                {
                    // Build the response object
                    var response = Request.CreateResponse<SubjectFull>(HttpStatusCode.Created, s);

                    // Set the Location header
                    // The "new Uri" object constructor needs a string argument
                    // The ApiController.Url property provides that
                    // Its Link() method takes two arguments...
                    // A route name, which can be seen in App_Start/WebApiConfig.cs, and
                    // A route value, which substitutes for the {id} placeholder
                    response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = s.Id }));

                    return response;
                }
            }
            else
            {
                return Request.CreateResponse(HttpStatusCode.UnsupportedMediaType);
            }
        }