public async Task <IHttpActionResult> CreateAsync(InstitutionDto institution) { _repository.Add(institution.Map <Institution>()); await _unitOfWork.CompleteAsync(); return(Created(new Uri(Request.RequestUri + "/" + institution.Id), institution)); }
public async Task <InstitutionDto> GetInstitutionAsync(Guid id, CancellationToken ct) { Institution InstitutionEntity = await _uow.Institutions.GetInstitutionAsync(id, ct); InstitutionDto InstitutionDto = _mapper.Mapper.Map <InstitutionDto>(InstitutionEntity); return(InstitutionDto); }
public void Add(InstitutionDto institutionDto) { Institution institution = new Institution() { InstitutionName = institutionDto.Name }; base.Insert(institution); }
public void Delete(InstitutionDto institutionDto) { Institution institution = new Institution() { InstitutionId = institutionDto.Id, InstitutionName = institutionDto.Name }; base.Delete(institution); }
public async Task <InstitutionDto> GetInstitutionByIdAsync(Guid id, CancellationToken ct) { InstitutionDto institutionDto = null; var institutionEntity = await _uow.Institutions.GetAsync(id, ct); if (institutionEntity != null) { institutionDto = _mapper.Map <InstitutionDto>(institutionEntity); } return(institutionDto); }
public async Task <int> UpdateAsync(InstitutionDto institution) { var institutionEntity = await _ctx.Institutions.FindAsync(institution.Id); if (institutionEntity == null) { return(0); } _mapper.Map(institution, institutionEntity); institutionEntity.UpdatedAt = DateTime.UtcNow; return(await _ctx.SaveChangesAsync()); }
public async Task <IActionResult> Put(Guid id, [FromBody] InstitutionDto institution) { if (!ModelState.IsValid || id != institution.Id) { return(BadRequest()); } if (await _institutionService.UpdateAsync(institution) != 1) { return(BadRequest()); } return(Ok()); }
public async Task <IHttpActionResult> UpdateAsync(int id, InstitutionDto institution) { var institutionInDb = await _repository.GetAsync(id); if (institutionInDb == null) { return(NotFound()); } _repository.Add(institution.Map <Institution>()); await _unitOfWork.CompleteAsync(); return(Ok()); }
public async Task <ActionResponse <InstitutionDto> > ModifyGoverningCouncil(InstitutionDto entityDto) { try { var response = await ActionResponse <InstitutionDto> .ReturnSuccess(entityDto, "Podaci o upravnom vijeću institucije uspješno ažurirani."); if (entityDto.GoverningCouncil != null) { if (entityDto.GoverningCouncil.Id.HasValue) { if ((await UpdateGoverningCouncil(entityDto.GoverningCouncil)) .IsNotSuccess(out ActionResponse <GoverningCouncilDto> councilResponse, out GoverningCouncilDto governingCouncil)) { return(await ActionResponse <InstitutionDto> .ReturnError(councilResponse.Message)); } entityDto.GoverningCouncil = governingCouncil; } else { if ((await InsertGoverningCouncil(entityDto.GoverningCouncil)) .IsNotSuccess(out ActionResponse <GoverningCouncilDto> councilResponse, out GoverningCouncilDto governingCouncil)) { return(await ActionResponse <InstitutionDto> .ReturnError(councilResponse.Message)); } entityDto.GoverningCouncil = governingCouncil; } } else { var institution = unitOfWork.GetGenericRepository <Institution>().FindBy(i => i.Id == entityDto.Id.Value, includeProperties: "GoverningCouncil"); if (institution.GoverningCouncil != null) { if ((await DeleteGoverningCouncil(institution.GoverningCouncil.Id)) .IsNotSuccess(out ActionResponse <GoverningCouncilDto> councilResponse)) { return(await ActionResponse <InstitutionDto> .ReturnError(councilResponse.Message)); } } } return(response); } catch (Exception) { return(await ActionResponse <InstitutionDto> .ReturnError("Greška prilikom ažuriranja podataka upravnog vijeća.")); } }
public async Task <InstitutionDto> UpdateInstitutionAsync(Guid id, UpdateInstitutionDto updateInstitutionDto, CancellationToken ct) { InstitutionDto institutionDto = null; var updatedEntity = _mapper.Map <Institution>(updateInstitutionDto); updatedEntity.Id = id; await _uow.Institutions.UpdateAsync(updatedEntity, id, ct, updatedEntity.RowVersion); await _uow.SaveChangesAsync(ct); var institutionEntity = await _uow.Institutions.GetAsync(id, ct); institutionDto = _mapper.Map <InstitutionDto>(institutionEntity); return(institutionDto); }
public async Task <InstitutionDto> CreateInstitutionAsync(Guid universityId, CreateInstitutionDto createInstitutionDto, CancellationToken ct) { InstitutionDto institutionDto = null; try { var institutionEntity = _mapper.Map <Institution>(createInstitutionDto); institutionEntity.UniversityId = universityId; await _uow.Institutions.AddAsync(institutionEntity, ct); await _uow.SaveChangesAsync(ct); institutionDto = _mapper.Map <InstitutionDto>(institutionEntity); } catch (AutoMapperMappingException ex) { _logger.LogError(ex, string.Empty); } return(institutionDto); }
public async Task <ActionResponse <InstitutionDto> > ModifyFiles(InstitutionDto entityDto) { try { var entity = unitOfWork.GetGenericRepository <Institution>() .FindBy(e => e.Id == entityDto.Id, includeProperties: "InstitutionFiles.File"); var currentFiles = mapper.Map <List <InstitutionFile>, List <InstitutionFileDto> >(entity.InstitutionFiles.ToList()); var newFiles = entityDto.Files; var filesToRemove = currentFiles .Where(cet => !newFiles.Select(f => f.Id).Contains(cet.FileId)).ToList(); var filesToAdd = newFiles .Where(nt => !currentFiles.Select(uec => uec.FileId).Contains(nt.Id)) .Select(sf => new InstitutionFileDto { FileId = sf.Id, InstitutionId = entityDto.Id }) .ToList(); if ((await RemoveFiles(filesToRemove)) .IsNotSuccess(out ActionResponse <List <InstitutionFileDto> > removeResponse)) { return(await ActionResponse <InstitutionDto> .ReturnError("Neuspješno micanje dokumenata instituciji.")); } if ((await AddFiles(filesToAdd)).IsNotSuccess(out ActionResponse <List <InstitutionFileDto> > addResponse, out filesToAdd)) { return(await ActionResponse <InstitutionDto> .ReturnError("Neuspješno dodavanje dokumenata instituciji.")); } return(await ActionResponse <InstitutionDto> .ReturnSuccess(entityDto, "Uspješno izmijenjeni dokumenti institucije.")); } catch (Exception) { return(await ActionResponse <InstitutionDto> .ReturnError("Greška prilikom izmjene dokumenata institucije.")); } }
public async Task <ActionResponse <InstitutionDto> > Insert(InstitutionDto institution) { try { using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) { List <FileDto> files = institution.Files != null ? new List <FileDto>(institution.Files) : new List <FileDto>(); GoverningCouncilDto governingCouncil = institution.GoverningCouncil; var institutionToAdd = mapper.Map <InstitutionDto, Institution>(institution); unitOfWork.GetGenericRepository <Institution>().Add(institutionToAdd); unitOfWork.Save(); institution.Id = institutionToAdd.Id; institution.Files = files; if ((await ModifyFiles(institution)) .IsNotSuccess(out ActionResponse <InstitutionDto> fileResponse, out institution)) { return(fileResponse); } institution.GoverningCouncil = governingCouncil; institution.GoverningCouncil.InstitutionId = institution.Id; if ((await ModifyGoverningCouncil(institution)).IsNotSuccess(out fileResponse, out institution)) { return(fileResponse); } scope.Complete(); } return(await GetInstitution()); } catch (Exception) { return(await ActionResponse <InstitutionDto> .ReturnError("Greška prilikom unosa podataka za instituciju.")); } }
public void Should_get_institution_list() { // // Arrange var testInstitution = new InstitutionDto { Id = Guid.NewGuid(), Name = "Test Institutions", Phone = "1-800-555-0000", Email = "*****@*****.**", Website = "http://test.com", CreatedAt = DateTime.UtcNow }; var mockInstitutionService = new Mock <IInstitutionService>(); mockInstitutionService .Setup(svc => svc.GetAll()) .Returns(new List <InstitutionDto> { testInstitution }); var institutionsController = new InstitutionsController(mockInstitutionService.Object); // // Act var result = institutionsController.Get(); // // Assert var okResult = Assert.IsType <OkObjectResult>(result); var model = Assert.IsAssignableFrom <IEnumerable <InstitutionDto> >(okResult.Value); var institutionResult = model.FirstOrDefault(); Assert.Equal(testInstitution.Id, institutionResult.Id); }
protected override void OnInitialized() { institutionDto = new InstitutionDto(); }
public async Task <ActionResponse <InstitutionDto> > Update([FromBody] InstitutionDto institution) { return(await institutionService.Update(institution)); }
protected override void OnInitialized() { institutionDto = UnitOfWork.Institutions.GetByID(int.Parse(id)); }