Exemple #1
0
        public IActionResult Create()
        {
            var model = new NewsletterCreateViewModel()
            {
                Title = "Upload A Newsletter"
            };

            return(View(model));
        }
Exemple #2
0
        public async Task <IActionResult> Create(NewsletterCreateViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            //if (model.Photo != null && model.Photo.Length > 0)
            //{
            //    if (!model.Photo.IsImage())
            //    {
            //        model.AlertMessage = "The uploaded file is not an image!";
            //        return View(model);
            //    }
            //}

            await _NewslettersService.InsertNewsletterAsync(model);

            return(RedirectToAction("List"));
        }
        public async Task <UploadedFile> InsertNewsletterAsync(NewsletterCreateViewModel model)
        {
            var ms = new MemoryStream();

            model.FormFile.CopyTo(ms);
            var newNewsletter = new UploadedFile()
            {
                Filename    = model.FormFile.FileName,
                CategoryId  = Dto.Enums.UploadedFileCategory.Newsletter,
                Filetype    = model.FormFile.ContentType,
                CreatedOn   = DateTime.Now,
                Description = model.Description,
                FileBytes   = ms.ToArray()
            };

            _UploadedFilesRepository.Insert(newNewsletter);
            await _UploadedFilesRepository.SaveChangesAsync();

            return(newNewsletter);
        }
Exemple #4
0
        public IActionResult Register([FromBody] NewsletterCreateViewModel model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    Newsletter newsletter = _newsletterRepository.FindUniqueByEmail(model.Email);

                    if (newsletter != null)
                    {
                        ModelState.AddModelError("Email", "Email já cadastrado");

                        IDictionary <string, string> error = new Dictionary <string, string>();
                        error.Add("email", "Email já cadastrado");

                        return(StatusCode(422, new {
                            StatusCode = 442,
                            Message = "Unprocessable entity",
                            Errors = error
                        }));
                    }

                    Newsletter obj = new Newsletter
                    {
                        Email     = model.Email,
                        Name      = model.Name,
                        CreatedAt = DateTime.Now,
                    };

                    _newsletterRepository.Add(obj);
                    _newsletterRepository.SaveChanges();

                    HttpContext.Response.StatusCode = 201;

                    return(StatusCode(201, new {
                        StatusCode = 201,
                        Message = "Created",
                        Data = obj
                    }));
                }

                IDictionary <string, string> errorList = new Dictionary <string, string>();

                foreach (var error in ModelState)
                {
                    if (error.Value.Errors.Any())
                    {
                        errorList.Add(error.Key, error.Value.Errors.First().ErrorMessage);
                    }
                }

                return(StatusCode(422, new {
                    StatusCode = 422,
                    Message = "Unprocessable entity",
                    Error = errorList
                }));
            }
            catch (Exception exception)
            {
                _logger.LogError("Newsletter api register error: " + exception);
                return(StatusCode(500));
            }
        }