public async Task <ServiceResult <bool> > Update(int id, InfoChannelMessageCreateOrUpdateRequestViewModel viewModel) { if (id <= 0) { throw new ArgumentException("Argument should be greater than 0", nameof(viewModel)); } var model = await _repository.GetSingleAsync(id); if (model == null) { return(ServiceResultFactory.Fail <bool>("Item not found")); } MapViewModelToModel(viewModel, model); var validator = new InfoChannelMessageCreateOrUpdateRequestViewModelValidator(); var validationResult = await validator.ValidateAsync(model); if (!validationResult.IsValid) { return(ServiceResultFactory.Fail <bool>(validationResult)); } _repository.Update(model); var changes = await _repository.SaveChangesAsync(); return(ServiceResultFactory.Success(changes > 0)); }
public async Task <ServiceResult <int> > Create(InfoChannelMessageCreateOrUpdateRequestViewModel viewModel) { var model = new InfoChannelMessage(); MapViewModelToModel(viewModel, model); model.Status = "1"; // TODO: What is status for? var validator = new InfoChannelMessageCreateOrUpdateRequestViewModelValidator(); var validationResult = await validator.ValidateAsync(model); if (!validationResult.IsValid) { return(ServiceResultFactory.Fail <int>(validationResult)); } await _repository.AddAsync(model); var changes = await _repository.SaveChangesAsync(); if (changes == 0) { return(ServiceResultFactory.Fail <int>("Insert fails")); } return(ServiceResultFactory.Success(model.Id)); }
private void MapViewModelToModel(InfoChannelMessageCreateOrUpdateRequestViewModel viewModel, InfoChannelMessage model) { model.Title = viewModel.Subject; model.Message = viewModel.Message; model.Visible = viewModel.IsVisible; }
public async Task <IActionResult> Update([FromRoute] int id, [FromBody] InfoChannelMessageCreateOrUpdateRequestViewModel viewModel) => await HandleResultAsync(() => _service.Update(id, viewModel));