Exemplo n.º 1
0
        public async Task <ActionResult <CreateUpdateContactDto> > PutAsync(CreateUpdateContactDto item)
        {
            try
            {
                await _service.UpdateAndSaveAsync(item);

                //NOTE: to get this to work you MUST set the name of the HttpGet, e.g. [HttpGet("{id}", Name= "GetSingleTodo")],
                //on the Get you want to call, then then use the Name value in the Response.
                //Otherwise you get a "No route matches the supplied values" error.
                //see https://stackoverflow.com/questions/36560239/asp-net-core-createdatroute-failure for more on this
                return(_service.Response(this, "GetSingleContact", new { id = item.Id }, item));
            }
            catch (DbUpdateException ex)
            {
                return(BadRequest(ex.InnerException.Message));
            }
        }
Exemplo n.º 2
0
        public async Task <ResultDto <Domain.Contact.Contact> > UpdateAsync(CreateUpdateContactDto dto, CancellationToken cancellationToken)
        {
            var validationResult = await _createUpdateContactDtoValidator.ValidateAsync(dto, cancellationToken);

            var result = new ResultDto <Domain.Contact.Contact>(validationResult);

            if (!validationResult.IsValid)
            {
                _logger.LogWarning("Contact update validation failed: {message}", validationResult.ToString());
                return(result);
            }

            var contact = await _contactRepository.UpdateByIdFrom(dto.ContactId.GetValueOrDefault(), dto);

            await _contactRepository.UnitOfWork.SaveChangesAsync(true, cancellationToken);

            result.AddData(contact);
            return(result);
        }
Exemplo n.º 3
0
        public async Task <IActionResult> OnPostImportAsync()
        {
            List <CreateUpdateContactDto> createUpdateContactDtos = new List <CreateUpdateContactDto>();
            IFormFile excel = Request.Form.Files[0];

            if (excel == null)
            {
                return(NoContent());
            }

            StringBuilder sb = new StringBuilder();

            sb.Append("<table class='table table-bordered table-hover'><tr>");
            sb.Append("<th>Email</th>");
            sb.Append("<th>First Name</th>");
            sb.Append("<th>Last Name</th>");
            sb.Append("<th>Date of birth</th>");
            sb.Append("<th>PhoneNumber</th>");
            sb.Append("<th>Addition</th>");
            sb.Append("</tr>");


            using (var workbook = new XLWorkbook(excel.OpenReadStream()))
            {
                var worksheet = workbook.Worksheet("Contact");
                var count     = 0;
                foreach (var row in worksheet.Rows())
                {
                    count += 1;
                    if (count > 1) //skip the first row.
                    {
                        if (row.Cell(1).Value.Equals("") || row.Cell(2).Value.Equals("") ||
                            row.Cell(3).Value.Equals("") || row.Cell(4).Value.Equals(""))
                        {
                            break;
                        }
                        var groupName = row.Cell(7).Value.ToString();
                        var group     = await _groupRepository.FindByNameAsync(groupName);

                        var createContactDto = new CreateUpdateContactDto()
                        {
                            GroupIds = new List <Guid>()
                            {
                                group.Id
                            },
                            Email       = row.Cell(1).Value.ToString(),
                            FirstName   = row.Cell(2).Value.ToString(),
                            LastName    = row.Cell(3).Value.ToString(),
                            DateOfBirth = row.Cell(4).Value.To <DateTime>(),
                            PhoneNumber = row.Cell(5).Value == null ? "" : row.Cell(5).Value.ToString(),
                            Addition    = row.Cell(6).Value == null ? "" : row.Cell(6).Value.ToString(),
                            Status      = 0
                        };
                        try
                        {
                            await _contactAppService.CreateAsync(
                                createContactDto
                                );

                            sb.AppendLine("<tr>");
                            sb.Append("<td>" + createContactDto.Email + "</td>");
                            sb.Append("<td>" + createContactDto.FirstName + "</td>");
                            sb.Append("<td>" + createContactDto.LastName + "</td>");
                            sb.Append("<td>" + createContactDto.DateOfBirth + "</td>");
                            sb.Append("<td>" + createContactDto.PhoneNumber + "</td>");
                            sb.Append("<td>" + createContactDto.Addition + "</td>");
                            sb.AppendLine("</tr>");
                        }
                        catch (Exception ex)
                        {
                            sb.AppendLine($"<tr class='table-danger'>");
                            sb.Append("<td>" + createContactDto.Email + "</td>");
                            sb.Append("<td>" + createContactDto.FirstName + "</td>");
                            sb.Append("<td>" + createContactDto.LastName + "</td>");
                            sb.Append("<td>" + createContactDto.DateOfBirth + "</td>");
                            sb.Append("<td>" + createContactDto.PhoneNumber + "</td>");
                            sb.Append("<td>" + createContactDto.Addition + "</td>");
                            sb.AppendLine("</tr>");
                            Console.WriteLine(ex);
                        }
                    }
                }

                //View result ajax
                sb.Append("</table>");

                return(this.Content(sb.ToString()));
            }
        }