Ejemplo n.º 1
0
        public async Task <IActionResult> AddQoute()
        {
            var qouteDTO = new QouteDTO
            {
                OutputHandler = new OutputHandler
                {
                    IsErrorOccured = false
                }
            };

            return(View(qouteDTO));
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> Update(QouteDTO qoute)
        {
            var output = await _qouteService.UpdateQoute(qoute);

            if (output.IsErrorOccured)
            {
                return(BadRequest(output));
            }
            else
            {
                return(Ok(output));
            }
        }
Ejemplo n.º 3
0
        public async Task <OutputHandler> CreateQoute(QouteDTO qouteDTO)
        {
            try
            {
                var outputhandler = await FileHandler.SaveFileFromByte(qouteDTO.ImgBytes, qouteDTO.FileName, FolderName);

                if (outputhandler.IsErrorOccured)
                {
                    if (outputhandler.IsErrorKnown)
                    {
                        return(new OutputHandler
                        {
                            IsErrorOccured = true,
                            Message = outputhandler.Message
                        });
                    }
                    return(new OutputHandler
                    {
                        IsErrorOccured = true,
                        Message = "Something went wrong while the system tried to upload the file"
                    });
                }
                qouteDTO.QouteImg = outputhandler.ImageUrl;
                var mapped = new AutoMapper <QouteDTO, Qoute>().MapToObject(qouteDTO);

                _qouteRepository.CreateEntity(mapped);
                await _qouteRepository.SaveChangesAsync();

                return(new OutputHandler
                {
                    IsErrorOccured = false,
                    Message = "Qoute Created Successfully"
                });
            }
            catch (Exception ex)
            {
                var outputHandler = await FileHandler.DeleteFileFromFolder(qouteDTO.QouteImg, FolderName);

                return(StandardMessages.getExceptionMessage(ex));
            }
        }
Ejemplo n.º 4
0
        public async Task <FileResult> DownloadFile(int qouteId)
        {
            var requestUrl = $"{BaseUrl}{apiUriQoute}/GetQoute?qouteId={qouteId}";
            var qouteDTO   = new QouteDTO
            {
                OutputHandler = new OutputHandler
                {
                    IsErrorOccured = false
                }
            };

            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri(requestUrl);
                HttpResponseMessage result = await client.GetAsync(requestUrl);

                if (result.StatusCode == HttpStatusCode.OK)
                {
                    qouteDTO = await result.Content.ReadAsAsync <QouteDTO>();

                    var outputHandler = new OutputHandler
                    {
                        IsErrorOccured = true
                    };
                    qouteDTO.OutputHandler = outputHandler;
                }
                else
                {
                    qouteDTO.OutputHandler = await result.Content.ReadAsAsync <OutputHandler>();
                }
            };


            //qoute.QouteImg = qoute.Artwork;
            //Send the File to Download.
            var filename = Path.GetFileName(qouteDTO.QouteImg);

            return(File(qouteDTO.ImgBytes, "application/octet-stream", filename));
        }
Ejemplo n.º 5
0
        public async Task <IActionResult> UpdateQoute(int qouteId)
        {
            var requestUrl = $"{BaseUrl}{apiUriQoute}/GetQoute?qouteId={qouteId}";
            var qouteDTO   = new QouteDTO
            {
                OutputHandler = new OutputHandler
                {
                    IsErrorOccured = false
                }
            };

            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri(requestUrl);
                HttpResponseMessage result = await client.GetAsync(requestUrl);

                if (result.StatusCode == HttpStatusCode.OK)
                {
                    qouteDTO = await result.Content.ReadAsAsync <QouteDTO>();

                    var outputHandler = new OutputHandler
                    {
                        IsErrorOccured = true
                    };
                    qouteDTO.OutputHandler = outputHandler;
                }
                else
                {
                    qouteDTO.OutputHandler = await result.Content.ReadAsAsync <OutputHandler>();
                }
            };


            //qoute.QouteImg = qoute.Artwork;
            return(View(qouteDTO));
        }
Ejemplo n.º 6
0
        public async Task <IActionResult> UpdateQoute(QouteDTO qouteDTO, IFormFile artwork)
        {
            try
            {
                var user = await StaticDataHandler.GetSessionDetails();

                qouteDTO.ModifiedBy   = user.Username;
                qouteDTO.ModifiedDate = DateTime.Now.AddHours(2);
                if (ModelState.IsValid)
                {
                    if (artwork == null)
                    {
                    }
                    else
                    {
                        var fileUploadResult = await StaticDataHandler.fileUpload(artwork, FolderName, qouteDTO.IsFeaturedOnHomePage);

                        if (fileUploadResult.IsErrorOccured)
                        {
                            return(View(qouteDTO.OutputHandler = new OutputHandler
                            {
                                IsErrorOccured = true,
                                Message = "Something went wrong"
                            }));
                        }
                        else
                        {
                            qouteDTO.ImgBytes = (byte[])fileUploadResult.Result; //return the byte data
                            qouteDTO.FileName = artwork.FileName;
                        }
                    }
                    var requestUrl = $"{BaseUrl}{apiUriQoute}/UpdateQoute";
                    using (var client = new HttpClient())
                    {
                        client.BaseAddress = new Uri(requestUrl);
                        client.DefaultRequestHeaders.Accept.Clear();
                        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", user.Token);

                        var result = await client.PutAsJsonAsync(client.BaseAddress, qouteDTO);

                        if (result.StatusCode == HttpStatusCode.OK)
                        {
                            return(RedirectToAction("ManageQoutes"));
                        }
                        else if (result.StatusCode == HttpStatusCode.Unauthorized)
                        {
                            qouteDTO.OutputHandler = new OutputHandler
                            {
                                IsErrorOccured = true,
                                Message        = "You're not Authorized to perfom this task"
                            };
                        }
                        else
                        {
                            qouteDTO.OutputHandler = await result.Content.ReadAsAsync <OutputHandler>();
                        }
                    };
                }
                else
                {
                }
            }
            catch (Exception ex)
            {
                if (ex.InnerException.Message != null)
                {
                    ModelState.AddModelError("", $"ERROR: {ex.InnerException.Message}");
                }
                else
                {
                    ModelState.AddModelError("", ex.Message);
                }
            }

            return(View(qouteDTO));
        }
Ejemplo n.º 7
0
        public async Task <OutputHandler> UpdateQoute(QouteDTO qouteDTO)
        {
            try
            {
                if (qouteDTO.ImgBytes == null)
                {
                    qouteDTO.QouteImg = qouteDTO.QouteImg;
                }
                else
                {
                    var outputhandler = await FileHandler.SaveFileFromByte(qouteDTO.ImgBytes, qouteDTO.FileName, FolderName);

                    if (outputhandler.IsErrorOccured)
                    {
                        if (outputhandler.IsErrorKnown)
                        {
                            return(new OutputHandler
                            {
                                IsErrorOccured = true,
                                Message = outputhandler.Message
                            });
                        }
                        return(new OutputHandler
                        {
                            IsErrorOccured = true,
                            Message = "Something went wrong while the system tried to upload the file"
                        });
                    }
                    qouteDTO.QouteImg = outputhandler.ImageUrl;
                }
                var mapped = new AutoMapper <QouteDTO, Qoute>().MapToObject(qouteDTO);
                await _qouteRepository.UpdateAsync(mapped);

                await _qouteRepository.SaveChangesAsync();

                if (qouteDTO.OldImageUrl == null)
                {
                }
                else
                {
                    if (qouteDTO.ImgBytes == null) //if Byte[] is null means image is not being updated
                    {
                    }
                    else // only delete if artwork is not null meaning image is being updated
                    //delete old file
                    {
                        var outputHandler = await FileHandler.DeleteFileFromFolder(qouteDTO.OldImageUrl, FolderName);

                        if (outputHandler.IsErrorOccured) //True means Delete was not successful for some reason
                        {
                            return(new OutputHandler
                            {
                                IsErrorKnown = true,
                                IsErrorOccured = true,
                                Message = "Qoute Details updated successfully, but deleting of old file failed, please alert Techarch Team"
                            });
                        }
                    }
                }

                return(new OutputHandler
                {
                    IsErrorOccured = false,
                    Message = "Qoute Updated Successfully"
                });
            }
            catch (Exception ex)
            {
                return(StandardMessages.getExceptionMessage(ex));
            }
        }