Пример #1
0
        public async Task <IHttpActionResult> Put(int id, [FromBody] FAQDtoModel faq)
        {
            if (faq == null)
            {
                return(BadRequest("Required data was not supplied."));
            }

            if (!await Exists(id))
            {
                return(NotFound());
            }

            var record = await db.FAQs.FindAsync(id);

            if (faq.Question != null && record.Question != faq.Question)
            {
                record.Question = faq.Question;
            }

            if (faq.Answer != null && record.Answer != faq.Answer)
            {
                record.Answer = faq.Answer;
            }

            if (faq.FAQCategory.ID != null && record.FAQCategoryID != faq.FAQCategory.ID)
            {
                record.FAQCategoryID = (int)faq.FAQCategory.ID;
            }

            return(await SaveChanges(record, ActionType.Put));
        }
Пример #2
0
        public async Task <IHttpActionResult> Post([FromBody] FAQDtoModel faq)
        {
            if (faq == null || faq.FAQCategory?.ID == null)
            {
                return(BadRequest("Required data was not supplied."));
            }

            if (await Exists(faq.Question, faq.Answer))
            {
                return(Conflict());
            }

            var record = Mapper.Map <FAQModel>(faq);

            record.FAQCategoryID = (int)faq.FAQCategory.ID;
            record.FAQCategory   = null; // Otherwise, it will try to create a new category

            db.FAQs.Add(record);

            return(await SaveChanges(record, ActionType.Post));
        }