Exemplo n.º 1
0
        public async Task <IActionResult> UpdateWord(int wordId, [FromForm] WordCreateDTO word)
        {
            try
            {
                var wordFromDb = await _repo.GetOneWithConditionTracking <Word>(word => word.Id == wordId);

                if (wordFromDb == null)
                {
                    return(NotFound());
                }
                _mapper.Map(word, wordFromDb);
                if (word.File != null)
                {
                    if (!string.IsNullOrEmpty(wordFromDb.PublicId))
                    {
                        var deleteResult = _cloud.DeleteImage(wordFromDb.PublicId);
                        if (deleteResult)
                        {
                            var uploadResult = _cloud.UploadImage(word.File);
                            if (uploadResult != null)
                            {
                                wordFromDb.WordImg  = uploadResult.PublicUrl;
                                wordFromDb.PublicId = uploadResult.PublicId;
                            }
                        }
                    }
                }
                if (word.Audio != null)
                {
                    var audioUploadResult = await _dropbox.UploadFile(word.Audio, "/Engrisk");

                    if (audioUploadResult != null)
                    {
                        wordFromDb.WordVoice = audioUploadResult.SharedUrl;
                    }
                }
                if (await _repo.SaveAll())
                {
                    return(Ok());
                }
                return(NoContent());
            }
            catch (System.Exception e)
            {
                throw e;
            }
        }
Exemplo n.º 2
0
        public async Task <IActionResult> CreateWord([FromForm] WordCreateDTO wordDTO)
        {
            var properties = new Dictionary <dynamic, dynamic>();

            if (string.IsNullOrEmpty(wordDTO.Eng))
            {
                return(BadRequest(new
                {
                    Error = "Không được để trống fields"
                }));
            }
            properties.Add("Eng", wordDTO.Eng.ToLower());
            if (_repo.Exists <Word>(properties))
            {
                return(Conflict(new
                {
                    Error = "Từ vựng bị trùng"
                }));
            }
            var word = _mapper.Map <Word>(wordDTO);

            if (wordDTO.File != null)
            {
                var result = _cloud.UploadImage(wordDTO.File);
                if (result != null)
                {
                    word.WordImg  = result.PublicUrl;
                    word.PublicId = result.PublicId;
                }
            }
            if (wordDTO.Audio != null)
            {
                var uploadResult = await _dropbox.UploadFile(wordDTO.Audio, "/Engrisk");

                if (uploadResult != null)
                {
                    word.WordVoice = uploadResult.SharedUrl;
                }
            }
            _repo.Create(word);
            if (await _repo.SaveAll())
            {
                return(CreatedAtAction("GetWord", new { id = word.Id }, word));
            }
            return(BadRequest("Error on creating word"));
        }