public async Task <ActionResult <PhraseVM> > CreatePhrase(PhraseVM phraseVM)
        {
            try
            {
                if (phraseVM == null)
                {
                    return(BadRequest());
                }

                // Add custom model validation error
                Phrase phrase = await phraseRepository.GetPhraseByname(phraseVM.Phrase);

                if (phrase != null)
                {
                    ModelState.AddModelError("Name", $"Phrase name: {phraseVM.Phrase.Text} already in use");
                    return(BadRequest(ModelState));
                }

                await phraseRepository.CreatePhrase(phraseVM);

                return(CreatedAtAction(nameof(GetPhrase),
                                       new { id = phraseVM.Phrase.Id }, phraseVM));
            }
            catch (DbUpdateException Ex)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError,
                                  Ex.InnerException.Message));
            }
        }
Example #2
0
        public async Task <PhraseVM> CreatePhrase(PhraseVM phraseVM)
        {
            var result = await appDbContext.Phrases.AddAsync(phraseVM.Phrase);

            foreach (string item in phraseVM.Phrase.Text.Split(' '))
            {
                Voc voc = await appDbContext.Vocs.FirstOrDefaultAsync(x => x.Text == item);

                if (voc != null)
                {
                    if (phraseVM.Phrase.VocsPhrases == null)
                    {
                        phraseVM.Phrase.VocsPhrases = new List <VocsPhrases>();
                    }
                    phraseVM.Phrase.VocsPhrases.Add(new VocsPhrases {
                        VocId = voc.Id
                    });
                }
            }

            await appDbContext.SaveChangesAsync();

            result.Entity.VocsPhrases = null;
            phraseVM.Phrase           = result.Entity;
            return(phraseVM);
        }
Example #3
0
        public async Task <PhraseVM> GetPhrase(int id)
        {
            PhraseVM phraseVM = new PhraseVM();

            phraseVM.Phrase = await appDbContext.Phrases.FirstOrDefaultAsync(e => e.Id == id);

            return(phraseVM);
        }
Example #4
0
        private async Task <PhraseVM> CheckDeserialize(HttpResponseWrapper <PhraseVM> httpResponseWrapper)
        {
            PhraseVM phraseVM = new PhraseVM();

            if (httpResponseWrapper.Success)
            {
                phraseVM = await Deserialize <PhraseVM>(httpResponseWrapper.HttpResponseMessage, defaultJsonSerializerOptions);
            }
            else
            {
                phraseVM.Exception = await httpResponseWrapper.GetBody();
            }

            return(phraseVM);
        }
Example #5
0
        public async Task <PhraseVM> UpdatePhrase(PhraseVM phraseVM)
        {
            Phrase result = await appDbContext.Phrases
                            .FirstOrDefaultAsync(e => e.Id == phraseVM.Phrase.Id);

            if (result != null)
            {
                appDbContext.Entry(result).State = EntityState.Detached;
                result = mapper.Map(phraseVM.Phrase, result);
                appDbContext.Entry(result).State = EntityState.Modified;

                await appDbContext.SaveChangesAsync();

                return(new PhraseVM {
                    Phrase = result
                });
            }

            return(null);
        }
        public async Task <ActionResult <PhraseVM> > UpdatePhrase(int id, PhraseVM phraseVM)
        {
            try
            {
                if (id != phraseVM.Phrase.Id)
                {
                    return(BadRequest("Phrase ID mismatch"));
                }

                // Add custom model validation error
                Phrase phrase = await phraseRepository.GetPhraseByname(phraseVM.Phrase);

                if (phrase != null)
                {
                    ModelState.AddModelError("Name", $"Phrase name: {phraseVM.Phrase.Text} already in use");
                    return(BadRequest(ModelState));
                }

                var phraseToUpdate = await phraseRepository.GetPhrase(id);

                if (phraseToUpdate == null)
                {
                    return(NotFound($"Phrase with Id = {id} not found"));
                }

                await phraseRepository.UpdatePhrase(phraseVM);

                return(CreatedAtAction(nameof(GetPhrase), new { id = phraseVM.Phrase.Id }, phraseVM));
            }

            catch (DbUpdateException Ex)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError,
                                  Ex.InnerException.Message));
            }
        }
Example #7
0
        public async Task <PhraseVM> UpdatePhrase(int id, PhraseVM phraseVM)
        {
            var response = await httpService.Put($"{url}/{id}", phraseVM);

            return(await CheckDeserialize(response));
        }
Example #8
0
        public async Task <PhraseVM> CreatePhrase(PhraseVM phraseVM)
        {
            var response = await httpService.PostAsync(url, phraseVM);

            return(await CheckDeserialize(response));
        }