public IHttpActionResult CreatePlace(string token, JDE_Places item, int UserId) { if (token != null && token.Length > 0) { var tenants = db.JDE_Tenants.Where(t => t.TenantToken == token.Trim()); if (tenants.Any()) { item.TenantId = tenants.FirstOrDefault().TenantId; item.PlaceToken = Static.Utilities.GetToken(); db.JDE_Places.Add(item); db.SaveChanges(); JDE_Logs Log = new JDE_Logs { UserId = UserId, Description = "Utworzenie zasobu", TenantId = tenants.FirstOrDefault().TenantId, Timestamp = DateTime.Now, NewValue = new JavaScriptSerializer().Serialize(item) }; db.JDE_Logs.Add(Log); db.SaveChanges(); return(Ok(item)); //return CreatedAtRoute("DefaultApi", new { id = item.PlaceId }, item); } else { return(NotFound()); } } else { return(NotFound()); } }
public HttpResponseMessage CreatePlace(string token, string PlaceJson, int UserId) { 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_Places item = jss.Deserialize <JDE_Places>(PlaceJson); 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")); } //create unique token unless the file already exists item.PlaceToken = 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.PlaceToken + ext.ToLower()}"; postedFile.SaveAs(filePath); Static.Utilities.ProduceThumbnail(filePath); item.Image = item.PlaceToken + ext.ToLower(); } } item.TenantId = tenants.FirstOrDefault().TenantId; item.CreatedOn = DateTime.Now; db.JDE_Places.Add(item); db.SaveChanges(); JDE_Logs Log = new JDE_Logs { UserId = UserId, Description = "Utworzenie zasobu", TenantId = tenants.FirstOrDefault().TenantId, Timestamp = DateTime.Now, NewValue = new JavaScriptSerializer().Serialize(item) }; db.JDE_Logs.Add(Log); db.SaveChanges(); return(Request.CreateResponse(HttpStatusCode.Created, item)); } else { return(Request.CreateResponse(HttpStatusCode.NotFound)); } } else { return(Request.CreateResponse(HttpStatusCode.NotFound)); } }
public IHttpActionResult EditPlace(string token, int id, JDE_Places item, int UserId, bool DeleteImage = false) { if (token != null && token.Length > 0) { var tenants = db.JDE_Tenants.Where(t => t.TenantToken == token.Trim()); if (tenants.Any()) { var items = db.JDE_Places.AsNoTracking().Where(u => u.TenantId == tenants.FirstOrDefault().TenantId&& u.PlaceId == id); if (items.Any()) { JDE_Logs Log = new JDE_Logs { UserId = UserId, Description = "Edycja zasobu", TenantId = tenants.FirstOrDefault().TenantId, Timestamp = DateTime.Now, OldValue = new JavaScriptSerializer().Serialize(items.FirstOrDefault()), NewValue = new JavaScriptSerializer().Serialize(item) }; db.JDE_Logs.Add(Log); item.LmBy = UserId; item.LmOn = DateTime.Now; // Check if to remove image if (DeleteImage) { string oFileName = item.Image; if (!string.IsNullOrEmpty(oFileName)) { // There was a file, must delete it first System.IO.File.Delete(Path.Combine(RuntimeSettings.Path2Files, oFileName)); System.IO.File.Delete(Path.Combine(RuntimeSettings.Path2Thumbs, oFileName)); } item.Image = null; } db.Entry(item).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!JDE_PlacesExists(id)) { return(NotFound()); } else { throw; } } } } } return(StatusCode(HttpStatusCode.NoContent)); }
public HttpResponseMessage ArchivePlace(string token, int id, int UserId) { if (token != null && token.Length > 0) { var tenants = db.JDE_Tenants.Where(t => t.TenantToken == token.Trim()); if (tenants.Any()) { var items = db.JDE_Places.AsNoTracking().Where(u => u.TenantId == tenants.FirstOrDefault().TenantId&& u.PlaceId == id); if (items.Any()) { JDE_Places orgItem = items.FirstOrDefault(); orgItem.IsArchived = true; JDE_Logs Log = new JDE_Logs { UserId = UserId, Description = "Archiwizacja zasobu", TenantId = tenants.FirstOrDefault().TenantId, Timestamp = DateTime.Now, OldValue = new JavaScriptSerializer().Serialize(items.FirstOrDefault()), NewValue = "" }; db.JDE_Logs.Add(Log); try { db.Entry(orgItem).State = EntityState.Modified; db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!JDE_PlacesExists(id)) { return(Request.CreateResponse(HttpStatusCode.NotFound)); } else { throw; } } catch (Exception ex) { return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex)); } } } } return(Request.CreateResponse(HttpStatusCode.NoContent)); }
public IHttpActionResult GetPlacesBySetName(string token, string name, int UserId) { if (token != null && token.Length > 0) { var tenants = db.JDE_Tenants.Where(t => t.TenantToken == token.Trim()); if (tenants.Any()) { object returned = JDE_SetsExists(name, false); if (Type.GetTypeCode(returned.GetType()) == TypeCode.Int32) { //there's set of given name, return its places var items = (from pl in db.JDE_Places join st in db.JDE_Sets on pl.SetId equals st.SetId join ar in db.JDE_Areas on pl.AreaId equals ar.AreaId join us in db.JDE_Users on pl.CreatedBy equals us.UserId join t in db.JDE_Tenants on pl.TenantId equals t.TenantId where t.TenantId == tenants.FirstOrDefault().TenantId&& st.SetId == (int)returned orderby pl.CreatedOn descending select new { PlaceId = pl.PlaceId, Number1 = pl.Number1, Number2 = pl.Number2, Name = pl.Name, Description = pl.Description, AreaId = ar.AreaId, AreaName = ar.Name, SetId = st.SetId, SetName = st.Name, Priority = pl.Priority, CreatedOn = pl.CreatedOn, CreatedBy = us.UserId, CreatedByName = us.Name + " " + us.Surname, TenantId = t.TenantId, TenantName = t.TenantName, PlaceToken = pl.PlaceToken } ); return(Ok(items)); } else { //there's NO set of given name, create it //But first you have to assign it to "Nieokreślone" Area (create if have to) JDE_Areas nArea; JDE_Sets nSet; JDE_Logs Log; if (!db.JDE_Areas.Where(a => a.Name.Equals("Nieokreślone")).Any()) { //no are like that, let's create it. nArea = new JDE_Areas { Name = "Nieokreślone", Description = "Trafia tu wszystko co zostaje utworzone bez jasno określonego docelowego obszaru", TenantId = tenants.FirstOrDefault().TenantId, CreatedBy = UserId, CreatedOn = DateTime.Now }; db.JDE_Areas.Add(nArea); db.SaveChanges(); Log = new JDE_Logs { UserId = UserId, Description = "Utworzenie obszaru", TenantId = tenants.FirstOrDefault().TenantId, Timestamp = DateTime.Now, NewValue = new JavaScriptSerializer().Serialize(nArea) }; db.JDE_Logs.Add(Log); db.SaveChanges(); } else { //it exists, ret nArea = db.JDE_Areas.Where(a => a.Name.Equals("Nieokreślone")).FirstOrDefault(); } nSet = new JDE_Sets { Name = name.Trim(), Description = null, CreatedBy = UserId, CreatedOn = DateTime.Now, TenantId = tenants.FirstOrDefault().TenantId }; db.JDE_Sets.Add(nSet); db.SaveChanges(); Log = new JDE_Logs { UserId = UserId, Description = "Utworzenie instalacji", TenantId = tenants.FirstOrDefault().TenantId, Timestamp = DateTime.Now, NewValue = new JavaScriptSerializer().Serialize(nSet) }; db.JDE_Logs.Add(Log); db.SaveChanges(); JDE_Places nPlace = new JDE_Places { Name = name.Trim(), Description = null, PlaceToken = Utilities.uniqueToken(), AreaId = nArea.AreaId, SetId = nSet.SetId, CreatedBy = UserId, CreatedOn = DateTime.Now, TenantId = tenants.FirstOrDefault().TenantId }; db.JDE_Places.Add(nPlace); db.SaveChanges(); Log = new JDE_Logs { UserId = UserId, Description = "Utworzenie zasobu", TenantId = tenants.FirstOrDefault().TenantId, Timestamp = DateTime.Now, NewValue = new JavaScriptSerializer().Serialize(nPlace) }; db.JDE_Logs.Add(Log); db.SaveChanges(); var items = (from pl in db.JDE_Places join st in db.JDE_Sets on pl.SetId equals st.SetId join ar in db.JDE_Areas on pl.AreaId equals ar.AreaId join us in db.JDE_Users on pl.CreatedBy equals us.UserId join t in db.JDE_Tenants on pl.TenantId equals t.TenantId where t.TenantId == tenants.FirstOrDefault().TenantId&& st.SetId == nSet.SetId orderby pl.CreatedOn descending select new { PlaceId = pl.PlaceId, Number1 = pl.Number1, Number2 = pl.Number2, Name = pl.Name, Description = pl.Description, AreaId = ar.AreaId, AreaName = ar.Name, SetId = st.SetId, SetName = st.Name, Priority = pl.Priority, CreatedOn = pl.CreatedOn, CreatedBy = us.UserId, CreatedByName = us.Name + " " + us.Surname, TenantId = t.TenantId, TenantName = t.TenantName, PlaceToken = pl.PlaceToken } ); return(Ok(items)); } } else { return(NotFound()); } } else { return(NotFound()); } }
public HttpResponseMessage EditPlace(string token, int id, int UserId, string PlaceJson) { if (token != null && token.Length > 0) { var tenants = db.JDE_Tenants.Where(t => t.TenantToken == token.Trim()); if (tenants.Any()) { var items = db.JDE_Places.Where(u => u.TenantId == tenants.FirstOrDefault().TenantId&& u.PlaceId == id); if (items.Any()) { JavaScriptSerializer jss = new JavaScriptSerializer(); JDE_Places item = jss.Deserialize <JDE_Places>(PlaceJson); JDE_Places orgItem = items.FirstOrDefault(); //handle image var httpRequest = HttpContext.Current.Request; if (httpRequest.ContentLength > 0) { //there's a new content if (httpRequest.ContentLength > Static.RuntimeSettings.MaxFileContentLength) { return(Request.CreateResponse(HttpStatusCode.BadRequest, $"{item.Name} przekracza dopuszczalną wielość pliku ({Static.RuntimeSettings.MaxFileContentLength} MB) i został odrzucony")); } 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.PlaceToken + ext.ToLower()}"; string oFileName = db.JDE_Places.Where(p => p.PlaceId == id).FirstOrDefault().Image; if (!string.IsNullOrEmpty(oFileName)) { // There was a file, must delete it first System.IO.File.Delete(Path.Combine(RuntimeSettings.Path2Files, oFileName)); System.IO.File.Delete(Path.Combine(RuntimeSettings.Path2Thumbs, oFileName)); } postedFile.SaveAs(filePath); Static.Utilities.ProduceThumbnail(filePath); item.Image = item.PlaceToken + ext.ToLower(); } } JDE_Logs Log = new JDE_Logs { UserId = UserId, Description = "Edycja części", TenantId = tenants.FirstOrDefault().TenantId, Timestamp = DateTime.Now, OldValue = new JavaScriptSerializer().Serialize(items.FirstOrDefault()), NewValue = PlaceJson }; db.JDE_Logs.Add(Log); item.LmBy = UserId; item.LmOn = DateTime.Now; try { db.Entry(orgItem).CurrentValues.SetValues(item); db.Entry(orgItem).State = EntityState.Modified; db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!JDE_PlacesExists(id)) { return(Request.CreateResponse(HttpStatusCode.NotFound)); } else { throw; } } catch (Exception ex) { return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex)); } } } } return(Request.CreateResponse(HttpStatusCode.NoContent)); }