示例#1
0
        public object Put(QuoteAnswer model)
        {
            object json;

            try
            {
                model = repository.Update(model);

                json = new
                {
                    total   = 1,
                    data    = model,
                    success = true
                };
            }
            catch (Exception ex)
            {
                LogManager.Write("ERROR:" + Environment.NewLine + "\tMETHOD = " + this.GetType().FullName + "." + MethodBase.GetCurrentMethod().Name + Environment.NewLine + "\tMESSAGE = " + ex.Message);

                json = new
                {
                    message = ex.Message,
                    success = false
                };
            };

            return(json);
        }
示例#2
0
        public static QuoteAnswer ToQuoteAnswer(this QuoteAnswerViewModel viewModel)
        {
            var answer = new QuoteAnswer
            {
                IsRightAnswer = viewModel.IsRightAnswer,
            };

            switch (viewModel.Author)
            {
            case { Id: { } id } :
示例#3
0
        public async Task <IActionResult> AcceptAnswer(string trueAuthor, string chosenAuthor, string binaryAnswer)
        {
            var userId = _userManager.GetUserId(User);

            var user = await _context.Users
                       .Where(x => x.Id == userId)
                       .Include(x => x.AnsweredQuotes)
                       .SingleOrDefaultAsync();

            var quote = await _context.Quotes.FirstAsync(x => x.Author == trueAuthor);

            if (user == null || quote == null)
            {
                return(NotFound());
            }

            var quoteToAdd = new QuoteAnswer()
            {
                Author = trueAuthor,
                Text   = quote.Text
            };

            if (binaryAnswer == null)
            {
                quoteToAdd.AnswerAuthor = chosenAuthor;
            }
            else
            {
                if (binaryAnswer == "yes")
                {
                    quoteToAdd.AnswerAuthor = chosenAuthor;
                }
                else if (binaryAnswer == "no" && chosenAuthor != trueAuthor)
                {
                    quoteToAdd.AnswerAuthor = trueAuthor;
                }
                else if (binaryAnswer == "no" && chosenAuthor == trueAuthor)
                {
                    quoteToAdd.AnswerAuthor = chosenAuthor;
                }
            }

            user.AnsweredQuotes.Add(quoteToAdd);

            await _context.SaveChangesAsync();

            return(NoContent());
        }
示例#4
0
        public object Delete(QuoteAnswer model)
        {
            object json;

            try
            {
                bool result = repository.Remove(model);
                json = new
                {
                    success = result
                };
            } catch (Exception ex) {
                json = new
                {
                    success = false,
                    message = ex.Message
                };
            }

            return(json);
        }
示例#5
0
        public object GetAll()
        {
            var queryValues = Request.RequestUri.ParseQueryString();

            int page  = Convert.ToInt32(queryValues["page"]);
            int start = Convert.ToInt32(queryValues["start"]);
            int limit = Convert.ToInt32(queryValues["limit"]);
            int id    = Convert.ToInt32(queryValues["id"]);
            int orden = Convert.ToInt32(queryValues["orden"]);

            string strFieldFilters = queryValues["fieldFilters"];

            FieldFilters fieldFilters = new FieldFilters();

            if (!String.IsNullOrEmpty(strFieldFilters))
            {
                fieldFilters = JsonConvert.DeserializeObject <FieldFilters>(strFieldFilters);
            }


            #region Configuramos el orden de la consulta si se obtuvo como parametro
            string strOrder = !string.IsNullOrWhiteSpace(queryValues["sort"]) ? queryValues["sort"] : "";
            strOrder = strOrder.Replace('[', ' ');
            strOrder = strOrder.Replace(']', ' ');

            Sort sort;

            if (!string.IsNullOrWhiteSpace(strOrder))
            {
                sort = JsonConvert.DeserializeObject <Sort>(strOrder);
            }
            else
            {
                sort = new Sort();
            }
            #endregion

            string query = !string.IsNullOrWhiteSpace(queryValues["query"]) ? queryValues["query"] : "";

            int totalRecords = 0;

            try
            {
                if (id == 0)
                {
                    object json;
                    var    lista = repository.GetList(query, fieldFilters, sort, page, start, limit, ref totalRecords);

                    json = new
                    {
                        total   = totalRecords,
                        data    = lista,
                        success = true
                    };

                    return(json);
                }
                else
                {
                    QuoteAnswer model = repository.Get(id);

                    object json = new
                    {
                        data    = model,
                        success = true
                    };

                    return(json);
                }
            }
            catch (Exception ex)
            {
                LogManager.Write("ERROR:" + Environment.NewLine + "\tMETHOD = " + this.GetType().FullName + "." + MethodBase.GetCurrentMethod().Name + Environment.NewLine + "\tMESSAGE = " + ex.Message);

                object json = new
                {
                    message = ex.Message,
                    success = false
                };

                return(json);
            }
        }