Exemple #1
0
        public async Task <IActionResult> UpdateAvatar(int accountId, [FromForm] PhotoDTO photoDTO)
        {
            int userId = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value);

            if (userId != accountId)
            {
                return(Unauthorized());
            }
            var accountFromDb = await _repo.GetAccountDetail(accountId);

            if (accountFromDb == null)
            {
                return(NotFound());
            }
            if (!string.IsNullOrEmpty(accountFromDb.PublicId))
            {
                _cloud.DeleteImage(accountFromDb.PublicId);
            }
            var result = _cloud.UploadImage(photoDTO.File);

            accountFromDb.PhotoUrl = result.PublicUrl;
            accountFromDb.PublicId = result.PublicId;
            if (await _repo.SaveAll())
            {
                return(Ok());
            }
            return(StatusCode(500));
        }
 public async Task <IActionResult> CreateQuiz([FromForm] QuizCreateDTO quizCreateDTO)
 {
     try
     {
         var quiz = _mapper.Map <Quiz>(quizCreateDTO);
         if (quizCreateDTO.File != null)
         {
             var result = _cloud.UploadImage(quizCreateDTO.File);
             if (result != null)
             {
                 quiz.PublicId  = result.PublicId;
                 quiz.QuizPhoto = result.PublicUrl;
             }
             ;
         }
         _repo.Create(quiz);
         if (await _repo.SaveAll())
         {
             return(Ok());
         }
         return(NoContent());
     }
     catch (System.Exception e)
     {
         throw e;
     }
 }
        public async Task <IActionResult> CreateSection([FromForm] SectionCreateDTO sectionDTO)
        {
            var properties = new Dictionary <dynamic, dynamic>();

            properties.Add("SectionName", sectionDTO.SectionName);
            if (_repo.Exists <Section>(properties))
            {
                return(Conflict(new
                {
                    Error = "Section đã tồn tại"
                }));
            }
            var section = _mapper.Map <Section>(sectionDTO);
            var result  = _cloud.UploadImage(sectionDTO.File);

            if (result != null)
            {
                section.PhotoUrl = result.PublicUrl;
                section.PublicId = result.PublicId;
            }
            _repo.Create(section);
            if (await _repo.SaveAll())
            {
                return(Ok());
            }
            return(StatusCode(500));
        }
        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"));
        }
        public async Task <IActionResult> CreateQuestion([FromForm] QuestionCreateDTO questionDTO)
        {
            var properties = new Dictionary <dynamic, dynamic>();

            properties.Add("Content", questionDTO.Content);
            if (_repo.Exists <Question>(properties))
            {
                return(Conflict());
            }
            var question = _mapper.Map <Question>(questionDTO);

            if (questionDTO.File != null)
            {
                var result = _helper.UploadImage(questionDTO.File);
                if (result != null)
                {
                    question.PhotoUrl = result.PublicUrl;
                    question.PublicId = result.PublicId;
                }
            }
            if (questionDTO.Audio != null)
            {
                var uploadResult = await _dropBox.UploadFile(questionDTO.Audio, "/Engrisk");

                if (uploadResult != null)
                {
                    question.Audio = uploadResult.SharedUrl;
                }
            }
            _repo.Create(question);
            if (await _repo.SaveAll())
            {
                return(Ok());
            }
            return(BadRequest("Error on create question"));
        }
Exemple #6
0
        public async Task <IActionResult> CreateBanner([FromForm] BannerDTO banner)
        {
            var file   = banner.File;
            var result = _cloudHelper.UploadImage(file);

            if (result != null)
            {
                var createdBanner = new Banner()
                {
                    PublicId    = result.PublicId,
                    PhotoUrl    = result.PublicUrl,
                    IsPublished = false
                };
                _repo.Create(createdBanner);
                if (await _repo.SaveAll())
                {
                    return(Ok());
                }
                return(StatusCode(500));
            }
            return(StatusCode(500));
        }