public IHttpActionResult CreateFile(string token, JDE_Files item, int UserId, int?PlaceId = null, int?PartId = null, int?ProcessId = null, int?UserLogId = null) { if (token != null && token.Length > 0) { var tenants = db.JDE_Tenants.Where(t => t.TenantToken == token.Trim()); if (tenants.Any()) { try { item.TenantId = tenants.FirstOrDefault().TenantId; item.CreatedOn = DateTime.Now; item.CreatedBy = UserId; item.Token = Static.Utilities.GetToken(); db.JDE_Files.Add(item); db.SaveChanges(); JDE_Logs Log = new JDE_Logs { UserId = UserId, Description = "Utworzenie pliku", TenantId = tenants.FirstOrDefault().TenantId, Timestamp = DateTime.Now, NewValue = new JavaScriptSerializer().Serialize(item) }; db.JDE_Logs.Add(Log); db.SaveChanges(); JDE_FileAssigns FileAssing = new JDE_FileAssigns { FileId = item.FileId, CreatedBy = UserId, CreatedOn = DateTime.Now, TenantId = tenants.FirstOrDefault().TenantId, PartId = PartId, PlaceId = PlaceId, ProcessId = ProcessId, UserLogId = UserLogId }; string word = ""; if (PlaceId != null) { word = "zasobu"; } else if (PartId != null) { word = "części"; } else if (ProcessId != null) { word = "zgłoszenia"; } else { word = "logu użytkownika"; } db.JDE_FileAssigns.Add(FileAssing); db.SaveChanges(); JDE_Logs Log2 = new JDE_Logs { UserId = UserId, Description = $"Przypisanie pliku do {word}", TenantId = tenants.FirstOrDefault().TenantId, Timestamp = DateTime.Now, NewValue = new JavaScriptSerializer().Serialize(item) }; db.JDE_Logs.Add(Log2); db.SaveChanges(); return(Ok(item)); }catch (Exception ex) { return(InternalServerError(ex)); } } else { return(NotFound()); } } else { return(NotFound()); } }
public HttpResponseMessage CreateFile(string token, string FileJson, int UserId, int?PlaceId = null, int?PartId = null, int?ProcessId = null, int?UserLogId = null) { if (token != null && token.Length > 0) { var tenants = db.JDE_Tenants.Where(t => t.TenantToken == token.Trim()); if (tenants.Any()) { JavaScriptSerializer jss = new JavaScriptSerializer(); JDE_Files item = jss.Deserialize <JDE_Files>(FileJson); var httpRequest = HttpContext.Current.Request; if (httpRequest.ContentLength > 0) { if (httpRequest.ContentLength > Static.RuntimeSettings.MaxFileContentLength) { return(Request.CreateResponse(HttpStatusCode.BadRequest, $"{item.Name} przekracza dopuszczalną wielość pliku ({Static.RuntimeSettings.MaxFileContentLength} MB) i został odrzucony")); } if (string.IsNullOrEmpty(item.Token)) { //create unique token unless the file already exists item.Token = Static.Utilities.GetToken(); item.TenantId = tenants.FirstOrDefault().TenantId; var postedFile = httpRequest.Files[0]; string filePath = ""; if (postedFile != null && postedFile.ContentLength > 0) { var ext = postedFile.FileName.Substring(postedFile.FileName.LastIndexOf('.')); filePath = $"{Static.RuntimeSettings.Path2Files}{item.Token + ext.ToLower()}"; postedFile.SaveAs(filePath); HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, item); } item.CreatedOn = DateTime.Now; item.CreatedBy = UserId; item.Link = filePath; db.JDE_Files.Add(item); db.SaveChanges(); JDE_Logs Log = new JDE_Logs { UserId = UserId, Description = "Utworzenie pliku", TenantId = tenants.FirstOrDefault().TenantId, Timestamp = DateTime.Now, NewValue = new JavaScriptSerializer().Serialize(item) }; db.JDE_Logs.Add(Log); db.SaveChanges(); } } JDE_FileAssigns FileAssing = new JDE_FileAssigns { FileId = item.FileId, CreatedBy = UserId, CreatedOn = DateTime.Now, TenantId = tenants.FirstOrDefault().TenantId, PartId = PartId, PlaceId = PlaceId, ProcessId = ProcessId, UserLogId = UserLogId }; string word = ""; if (PlaceId != null) { word = "zasobu"; } else if (PartId != null) { word = "części"; } else if (ProcessId != null) { word = "zgłoszenia"; } else { word = "logu użytkownika"; } db.JDE_FileAssigns.Add(FileAssing); db.SaveChanges(); JDE_Logs Log2 = new JDE_Logs { UserId = UserId, Description = $"Przypisanie pliku do {word}", TenantId = tenants.FirstOrDefault().TenantId, Timestamp = DateTime.Now, NewValue = new JavaScriptSerializer().Serialize(item) }; db.JDE_Logs.Add(Log2); db.SaveChanges(); return(Request.CreateResponse(HttpStatusCode.OK, item)); } else { return(Request.CreateResponse(HttpStatusCode.NotFound)); } } else { return(Request.CreateResponse(HttpStatusCode.NotFound)); } }