public async Task <IHttpActionResult> PutVocabulary(int id, Vocabulary item)
        {
            VocabularyBL vocabularyBL = new VocabularyBL(_context);

            if (!id.Equals(item.vocaID))
            {
                return(BadRequest());
            }

            else if (!vocabularyBL.VocabularyExists(id))
            {
                var ItemId = await vocabularyBL.CreateNewVocabulary(item);

                if (ItemId == null)
                {
                    return(InternalServerError());
                }

                return(Created(Request.RequestUri, item));
            }
            var isUpdated = await vocabularyBL.UpdateVocabulary(item);

            if (isUpdated == false)
            {
                return(InternalServerError());
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
 public async Task <IHttpActionResult> GetVocabulary(string id_or_word)
 {
     try
     {
         VocabularyBL vocabularyDL = new VocabularyBL(_context);
         bool         isValid      = Int32.TryParse(id_or_word, out int ID);
         var          result       = new object();
         if (isValid)
         {
             result = await vocabularyDL.GetVocabularyById(ID);
         }
         else
         {
             result = await vocabularyDL.GetVocabularyByWord(id_or_word);
         }
         if (result == null)
         {
             return(NotFound());
         }
         return(Ok(result));
     }
     catch (Exception e)
     {
         return(Content(HttpStatusCode.InternalServerError, e.Message));
     }
 }
        public async Task <IHttpActionResult> PostVocabulary(Vocabulary item)
        {
            VocabularyBL vocabularyBL = new VocabularyBL(_context);
            var          Id           = await vocabularyBL.CreateNewVocabulary(item);

            if (Id == null)
            {
                return(InternalServerError());
            }

            return(Created(String.Format("{0}/{1}", Request.RequestUri, Id), item));
        }
        public async Task <IHttpActionResult> DeleteVocabulary(int id)
        {
            VocabularyBL vocabularyBL = new VocabularyBL(_context);

            if (!vocabularyBL.VocabularyExists(id))
            {
                return(NotFound());
            }

            var result = await vocabularyBL.DeleteVocabularyItem(id);

            if (result == null)
            {
                return(InternalServerError());
            }

            return(Ok(result));
        }
        public async Task <IHttpActionResult> GetVocabulariesByTopicID(string topicName)
        {
            try
            {
                VocabularyBL vocabularyDL = new VocabularyBL(_context);
                var          results      = await vocabularyDL.GetVocabulariesByTopicName(topicName);

                if (results.Count() == 0)
                {
                    return(NotFound());
                }
                return(Ok(results));
            }
            catch (Exception e)
            {
                return(Content(HttpStatusCode.InternalServerError, e.Message));
            }
        }
        public async Task <IHttpActionResult> GetVocabulariesWithPagination(int limit, int offset)
        {
            try
            {
                VocabularyBL vocabularyDL = new VocabularyBL(_context);
                var          results      = await vocabularyDL.GetVocabulariesPagination(limit, offset, "null");

                if (results.Count() == 0)
                {
                    return(Content(HttpStatusCode.OK, String.Format("No result for request")));
                }
                var shapedResults = results.Select(x => GetShapedObject(x, new string[0])).ToList();
                return(Ok(shapedResults));
            }
            catch (Exception e)
            {
                return(Content(HttpStatusCode.InternalServerError, e.Message));
            }
        }
        public async Task <IHttpActionResult> SearchInVocabulary(string q)
        {
            try
            {
                var          requestedKeyword = Regex.Replace(q, @"[^a-zA-Z0-9\s]+", "");
                VocabularyBL vocabularyDL     = new VocabularyBL(_context);
                var          results          = await vocabularyDL.SearchInVocabulary(q);

                if (results.Count() == 0)
                {
                    return(Content(HttpStatusCode.OK, "No result for request"));
                }
                return(Ok(results));
            }
            catch (Exception e)
            {
                return(Content(HttpStatusCode.InternalServerError, e.Message));
            }
        }
        public async Task <IHttpActionResult> GetVocabulariesWithFields(string fields)
        {
            try
            {
                VocabularyBL vocabularyDL = new VocabularyBL(_context);
                var          results      = await vocabularyDL.GetVocabularies();

                if (results.Count() == 0)
                {
                    return(Content(HttpStatusCode.OK, String.Format("No result for request")));
                }
                // Getting the fields is an expensive operation, so the default is all,
                // in which case we will just return the results
                if (!string.Equals(fields, "all", StringComparison.OrdinalIgnoreCase))
                {
                    var   propList        = typeof(Vocabulary).GetProperties();
                    var   props           = propList.Select(x => new KeyValuePair <string, PropertyInfo>(x.Name, x));
                    var   prop            = props.ToDictionary(x => x.Key, x => x.Value);
                    Regex regex           = new Regex(@"[^,+()]");
                    var   requestedFields = regex.Matches(fields).Cast <Match>().Select(m => m.Value).Distinct();
                    foreach (var field in requestedFields)
                    {
                        if (!prop.ContainsKey(field))
                        {
                            return(NotFound());
                        }
                        else if (field.Equals(requestedFields.Last()))
                        {
                            var shapedResults = results.Select(x => GetShapedObject(x, requestedFields)).ToList();
                            return(Ok(shapedResults));
                        }
                    }
                }
                return(Ok(results));
            }
            catch (Exception e)
            {
                return(Content(HttpStatusCode.InternalServerError, e.Message));
            }
        }