/// <summary> /// Updates basic information of a material /// </summary> /// <param name="updateMaterialDTO">UpdateMaterialDTO with the data regarding the material update</param> /// <returns>boolean true if the update was successful, false if not</returns> public bool updateMaterialBasicInformation(UpdateMaterialDTO updateMaterialDTO) { MaterialRepository materialRepository = PersistenceContext.repositories().createMaterialRepository(); Material materialBeingUpdated = materialRepository.find(updateMaterialDTO.id); bool updatedWithSuccess = true; bool perfomedAtLeastOneUpdate = false; if (updateMaterialDTO.reference != null) { updatedWithSuccess &= materialBeingUpdated.changeReference(updateMaterialDTO.reference); perfomedAtLeastOneUpdate = true; } if (updateMaterialDTO.designation != null) { updatedWithSuccess &= materialBeingUpdated.changeDesignation(updateMaterialDTO.designation); perfomedAtLeastOneUpdate = true; } if (updateMaterialDTO.image != null) { updatedWithSuccess &= materialBeingUpdated.changeImage(updateMaterialDTO.image); perfomedAtLeastOneUpdate = true; } if (!perfomedAtLeastOneUpdate || !updatedWithSuccess) { return(false); } updatedWithSuccess &= materialRepository.update(materialBeingUpdated) != null; return(updatedWithSuccess); }
public async Task ensureBasicInfoPutFailsForNonExistingMaterial() { UpdateMaterialDTO materialDTO = new UpdateMaterialDTO(); materialDTO.designation = "updated designation"; materialDTO.reference = "updated reference"; var response = await client.PutAsJsonAsync(String.Format(urlBase + "/{0}", -1), materialDTO); Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode); Assert.NotNull(response.Content.ReadAsStringAsync()); }
public FileModel UpdateMaterial(UpdateMaterialDTO data) { MaterialModel material = _context.Materials.Where(m => m.Id == data.MaterialId).FirstOrDefault(); FileModel file = new FileModel(); file.MaterialId = material.Id; file.Version = _context.Files.Where(f => f.MaterialId == material.Id).Max(f => f.Version) + 1; file.Size = (ulong)data.fileBlob.Length; file.Id = _context.Files.Count() > 0 ? _context.Files.Max(f => f.Id) + 1 : 1; file.Path = _fileService.UploadFile(material, file, data.fileBlob); file.UploadTime = DateTime.Now; _context.Add(file); _context.SaveChanges(); return(file); }
public async Task ensureBasicInfoPutSucceedsForValidBody() { var updatedDTO = new UpdateMaterialDTO(); updatedDTO.designation = "new designation"; updatedDTO.reference = "new reference"; updatedDTO.image = "newimg.png"; var materialDTO = ensurePostMaterialWorks(); materialDTO.Wait(); var response = await client.PutAsJsonAsync(String.Format(urlBase + "/{0}", materialDTO.Id), updatedDTO); Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.NotNull(response.Content.ReadAsStringAsync()); }
public ActionResult updateMaterialBasicInformation(long id, [FromBody] UpdateMaterialDTO updateMaterialData) { try { updateMaterialData.id = id; if (new core.application.MaterialsController().updateMaterialBasicInformation(updateMaterialData)) { return(Ok(new SimpleJSONMessageService(UPDATE_MATERIAL_SUCCESS))); } } catch (NullReferenceException) { return(BadRequest(new SimpleJSONMessageService(INVALID_REQUEST_BODY_MESSAGE))); } catch (Exception) { return(StatusCode(500, UNEXPECTED_ERROR)); } return(BadRequest(new SimpleJSONMessageService(UNABLE_TO_UPDATE_MATERIAL))); }
public string UpdateMaterial([FromForm] UpdateMaterialDTO fileInfo, [FromForm] IFormFile file) { try { byte[] bytes; using (var stream = new MemoryStream()) { file.CopyTo(stream); bytes = stream.ToArray(); } fileInfo.fileBlob = bytes; _materialService.UpdateMaterial(fileInfo); return("Ok"); } catch (Exception e) { return("NotOk"); } }