public async void CreateAsync_GivenValidAttachment_ReturnsNewAttachmentId() { var attachmentToCreate = new AttachmentCreateDTO { Description = "An example attachment", Data = "fuvwygwiu gbuywgykaguygdchjbaeiuyxgciuyadhviu bwrhjdsiyeabfcuyuw wyadvfjcvyut3er78t2euabdcbeaiyc eqdcgfw", Type = (int)AttachmentTypes.BITMAP, ProjectId = 1 }; var user = new User() { Id = 1, Firstname = "John", Surname = "Smith", AzureUId = "rfaweaw", Mail = "*****@*****.**" }; var project = new Project() { Id = 1, Title = "Foo", Description = "Bar", Creator = user, CreatedDate = System.DateTime.UtcNow }; context.Projects.Add(project); context.SaveChanges(); //SanityCheck Assert.NotNull(context.Projects.AsNoTracking().Where(p => p.Id == 1).First()); using (var repository = new AttachmentRepository(context)) { var id = await repository.CreateAsync(attachmentToCreate); Assert.Equal((await context.Attachments.FirstAsync()).Id, id); } }
public async Task <int> Create(AttachmentCreateDTO attachment) { var response = await _client.PostAsync("api/v1/attachments", attachment.ToHttpContent()); var newAttachmentId = response.Content.To <int>().Result; return(response.IsSuccessStatusCode ? newAttachmentId : -1); }
public async Task <(ResponseLogic, int)> CreateAsync(AttachmentCreateDTO attachment) { var result = await _repository.CreateAsync(attachment); if (result > 0) { return(ResponseLogic.SUCCESS, result); } else { return(ResponseLogic.ERROR_CREATING, 0); } }
public async Task <int> PostProject(CreateProjectDTO createProjectDTO, List <SkillCreateDTO> SkillsList, List <StorageFile> attachments) { if (account != null) { LocationCreateDTO locationCreateDTO = new LocationCreateDTO { Country = createProjectDTO.Location.Country, City = createProjectDTO.Location.City }; var locationID = await locationAPI.Create(locationCreateDTO); LocationDTO locationDTO = new LocationDTO { Country = createProjectDTO.Location.Country, City = createProjectDTO.Location.City, Id = locationID }; createProjectDTO.Location = locationDTO; //this should return the id var projectID = await projectAPI.Create(createProjectDTO); foreach (SkillCreateDTO skillCreateDTO in SkillsList) { var skillID = await skillAPI.Create(skillCreateDTO); //replace with actual id SkillDTO skillDTO = new SkillDTO { Name = skillCreateDTO.Name, Id = skillID }; await projectAPI.AddSkill(projectID, skillDTO); } foreach (StorageFile file in attachments) { byte[] imageArray = System.IO.File.ReadAllBytes(file.Path); string serializedData = Convert.ToBase64String(imageArray); var attachment = new AttachmentCreateDTO { Data = serializedData, Description = file.Name, ProjectId = projectID, Type = (int)AttachmentTypes.BITMAP }; await attachmentAPI.Create(attachment); } if (projectID != -1) { return(projectID); //var projectViewModel = new ProjectViewModel(initProjectDTO); //service.Navigate(typeof(ProjectPage), projectViewModel); } } return(-1); }
public async Task <IActionResult> Post([FromBody] AttachmentCreateDTO attachment) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var success = await _attachmentLogic.CreateAsync(attachment); if (success.Outcome == ResponseLogic.SUCCESS) { return(CreatedAtAction(nameof(Get), new { success.Id }, success.Id)); } else { return(StatusCode(500)); } }
public async void CreateAsync_GivenSaveChangesError_ReturnsDbUpdateException() { var attachmentToCreate = new AttachmentCreateDTO { Description = "An example attachment", Data = "fuvwygwiu gbuywgykaguygdchjbaeiuyxgciuyadhviu bwrhjdsiyeabfcuyuw wyadvfjcvyut3er78t2euabdcbeaiyc eqdcgfw", Type = (int)AttachmentTypes.BITMAP }; var contextMock = new Mock <ICrowdSparkContext>(); contextMock.Setup(c => c.SaveChangesAsync(default(CancellationToken))).ThrowsAsync(new DataException("error")); contextMock.Setup(c => c.Attachments.Add(It.IsAny <Attachment>())); using (var repository = new AttachmentRepository(contextMock.Object)) { await Assert.ThrowsAsync <DbUpdateException>(async() => await repository.CreateAsync(attachmentToCreate)); } }
public async Task <int> CreateAsync(AttachmentCreateDTO attachment) { var attachmentToCreate = new Attachment { Description = attachment.Description, Data = attachment.Data, Type = attachment.Type, ProjectId = attachment.ProjectId }; _context.Attachments.Add(attachmentToCreate); if (await saveContextChanges() > 0) { return(attachmentToCreate.Id); } else { throw new DbUpdateException("Error creating attachment", (Exception)null); } }