public async Task AddAsync(CreateHostDto dto) { await Task.CompletedTask; var host = new Host(dto.Name, dto.Description); _hostsRepository.Add(host); await _hostsRepository.UnitOfWork.SaveChangesAsync(); }
public async Task <IActionResult> Create([FromBody] CreateHostDto createHostDto) { var result = await _unitOfWork.HostService.Create(createHostDto); if (!result.Success) { return(result.ApiResult); } return(Created(Url.Link("GetHost", new { result.Data.Id }), _mapper.Map <HostDto>(result.Data))); }
public async Task <IActionResult> AddAsync([FromBody] CreateHostDto dto) { await _hostsService.AddAsync(dto); return(Accepted()); // CreateAtRoute || Action }
public async Task <Result <HostDto> > Create(CreateHostDto createHostDto) { var categories = new List <Category>(); if (createHostDto.Categories != null && createHostDto.Categories.Any()) { categories = await Context.Categories.Where(c => createHostDto.Categories.Contains(c.Id)) .ToListAsync(); if (categories.Count != createHostDto.Categories.Count) { return(Result <HostDto> .Failed(new NotFoundObjectResult( new ApiMessage { Message = ResponseMessage.CategoryNotFound }))); } } var host = _mapper.Map <Host>(createHostDto); host.EventCount = 0; host.Id = Guid.NewGuid(); host.HostCategories = categories.Select(c => new HostCategory { Category = c, Host = host }).ToList(); host.HostGalleries = createHostDto.Galleries?.Select(image => new HostGallery { Image = image, Host = host }).ToList(); host.HostAvailableDates = createHostDto.HostAvailableDates.Any() ? createHostDto.HostAvailableDates.Select(date => { var dateFromHour = date.FromHour; var dateToHour = date.ToHour; TimeSpan fromHour; TimeSpan toHour; if (TimeSpan.TryParse(dateFromHour, out fromHour)) { if (TimeSpan.TryParse(dateToHour, out toHour)) { return new HostAvailableDate { Days = date.Days, DateTime = date.DateTime, FromHour = fromHour, Host = host, HostId = host.Id, ToHour = toHour, Id = Guid.NewGuid() } } } ; return(null); }).Where(a => a != null).ToList() : new List <HostAvailableDate>(); await AddAsync(host); await Context.SaveChangesAsync(); return(Result <HostDto> .SuccessFull(_mapper.Map <HostDto>(host))); }