Пример #1
0
        public async Task Update(int id, CreateDeveloperDTO dto)
        {
            var developer = await _context.Developers.FindAsync(id);

            if (developer == null)
            {
                throw new EntityNotFoundException("Developer");
            }

            if (developer.Name != dto.Name)
            {
                developer.Name = dto.Name;
            }

            if (developer.HQ != dto.Name)
            {
                developer.HQ = dto.HQ;
            }

            if (developer.Founded != dto.Founded)
            {
                developer.Founded = (DateTime)dto.Founded;
            }

            if (developer.Website != dto.Website)
            {
                developer.Website = dto.Website;
            }

            _context.Entry(developer).State = EntityState.Modified;

            await _context.SaveChangesAsync();
        }
        public async Task <ActionResult> Create([FromForm] CreateDeveloperDTO dto)
        {
            var validator = new DeveloperFluentValidatior(_context);
            var errors    = await validator.ValidateAsync(dto);

            if (!errors.IsValid)
            {
                var mapped = errors.Errors.Select(x => new
                {
                    Name  = x.PropertyName,
                    Error = x.ErrorMessage
                }).ToArray();

                TempData["error"] = "Please fill all blank boxes.";                 //mapped.ToString();
                return(RedirectToAction(nameof(Create)));
            }
            try
            {
                // TODO: Add insert logic here
                await _developerService.Create(dto);

                return(RedirectToAction(nameof(Index)));
            }
            catch (Exception e)
            {
                TempData["error"] = "Exception";
                return(RedirectToAction(nameof(Index)));
            }
        }
        public async Task <ActionResult> Edit(int id, [FromForm] CreateDeveloperDTO dto)
        {
            var validator = new DevelopUpdateFluentValidator(_context, id);
            var errors    = await validator.ValidateAsync(dto);

            if (!errors.IsValid)
            {
                var mapped = errors.Errors.Select(x => new
                {
                    Name  = x.PropertyName,
                    Error = x.ErrorMessage
                }).ToArray();

                TempData["error"] = "Please fill all blank boxes.";                 //mapped.ToString();
                return(RedirectToAction(nameof(Create)));
            }
            try {
                // TODO: Add update logic here
                await _developerService.Update(id, dto);

                return(RedirectToAction(nameof(Index)));
            } catch (EntityNotFoundException e) {
                TempData["error"] = e.Message;
                return(RedirectToAction(nameof(Index)));
            } catch {
                return(View());
            }
        }
Пример #4
0
        public async Task Create(CreateDeveloperDTO dto)
        {
            await _context.Developers.AddAsync(new Domain.Developer
            {
                Name    = dto.Name,
                HQ      = dto.HQ,
                Founded = (DateTime)dto.Founded,
                Website = dto.Website
            });

            await _context.SaveChangesAsync();
        }
        public async Task <IActionResult> Post([FromBody] CreateDeveloperDTO dto)
        {
            var validator = new DeveloperFluentValidatior(_context);
            var errors    = await validator.ValidateAsync(dto);

            if (!errors.IsValid)
            {
                return(UnprocessableEntity(ValidationFormatter.Format(errors)));
            }

            try {
                await _developerService.Create(dto);

                return(StatusCode(201));
            } catch (Exception) {
                return(StatusCode(500, new { ServerErrorResponse.Message }));
            }
        }
        public async Task <IActionResult> Put(int id, [FromBody] CreateDeveloperDTO dto)
        {
            var validator = new DevelopUpdateFluentValidator(_context, id);
            var errors    = await validator.ValidateAsync(dto);

            if (!errors.IsValid)
            {
                return(UnprocessableEntity(ValidationFormatter.Format(errors)));
            }

            try {
                await _developerService.Update(id, dto);

                return(NoContent());
            } catch (EntityNotFoundException e) {
                return(NotFound(e.Message));
            } catch (Exception) {
                return(StatusCode(500, new { ServerErrorResponse.Message }));
            }
        }