public IHttpActionResult SaveModelPhoto(ModelPhoto photo) { EmpDbEntities entity = new EmpDbEntities(); PhotoTable exist = entity.PhotoTables.Where(d => d.EmployeeId == photo.EmployeeId).FirstOrDefault(); if (exist != null) { throw new Exception("Photo already exist"); } PhotoTable newData = new PhotoTable(); newData.Photo = photo.Photo; newData.EmployeeId = photo.EmployeeId; entity.PhotoTables.Add(newData); bool result = entity.SaveChanges() > 0; if (result) { return(Ok("Successfully Saved")); } else { return(BadRequest("Failed to Save")); } }
private dynamic GetPhotoById(dynamic parameters) { try { var placeId = (int?)parameters.id; if (placeId == null) { return(Response.AsJson("Wrong place id.", HttpStatusCode.BadRequest)); } var photoId = (int?)parameters.photoId; if (photoId == null) { return(Response.AsJson("Wrong photo id.", HttpStatusCode.BadRequest)); } var db = new MainContext(); ModelPhoto photo = ServicePlace.GetPhotoById(placeId, photoId, db); if (photo == null) { return(Response.AsJson("Photo or place does not exist.", HttpStatusCode.BadRequest)); } return(Response.AsJson(photo.GetView())); } catch (InDataError) { return(Response.AsJson("Internal server error", HttpStatusCode.InternalServerError)); } catch (NotContaining) { return(Response.AsJson("Photo not in place", HttpStatusCode.BadRequest)); } }
private ModelAllShoppe SetModelAllShope() { ModelAllShoppe modelAllShoppe = null; ModelProductDAO modelProduct = null; ModelPhoto modelPhoto = null; ModelDatePrice modelDate = null; modelAllShoppe = new ModelAllShoppe(); modelAllShoppe.shope = new List <ModelProductDAO>(); foreach (var product in ManagerShope.listProduct) { modelProduct = new ModelProductDAO(); modelPhoto = new ModelPhoto(); modelDate = new ModelDatePrice(); modelProduct.listPhoto = new List <ModelPhoto>(); modelProduct.ModelDatePrice = new List <ModelDatePrice>(); modelDate.dataTime = DateTime.Today.ToLongDateString(); modelDate.price = product.price; modelProduct.listPhoto.AddRange(DeserizerPhotoInStr(product.listPhoto)); modelProduct.ModelDatePrice.Add(modelDate); modelProduct.description = product.description; modelProduct.id = product.id; modelProduct.nameProduct = product.nameProduct; modelAllShoppe.shope.Add(modelProduct); } return(modelAllShoppe); }
private List <ModelPhoto> DeserizerPhotoInStr(List <string> listPhoto) { ModelPhoto modelPhoto = null; List <ModelPhoto> mListPhoto = new List <ModelPhoto>(); foreach (var photo in listPhoto) { modelPhoto = new ModelPhoto(); modelPhoto.photo = photo; mListPhoto.Add(modelPhoto); } return(mListPhoto); }
public static ModelPhoto AddPhoto(ModelPlace place, ModelUser user, string url, MainContext db) { if (!user.Equals(place.Author)) { throw new UnauthorizedAccessException(); } var photo = new ModelPhoto { Place = place, Url = url }; db.Photos.Add(photo); place.Photos.Add(photo); db.SaveChanges(); return(photo); }
public IHttpActionResult GetPhoto(long employeeId) { EmpDbEntities entity = new EmpDbEntities(); PhotoTable exist = entity.PhotoTables.Where(d => d.EmployeeId == employeeId).FirstOrDefault(); if (exist != null) { ModelPhoto model = new ModelPhoto(); model.Photo = exist.Photo; model.EmployeeId = employeeId; return(Ok(model)); } else { return(NotFound()); } }
private dynamic PostPhoto(dynamic parameters) { try { var userName = (string)Request.Session[ControllerUser.SessionUserNameKey]; if (string.IsNullOrEmpty(userName)) { return(Response.AsJson("Noone logged in.", HttpStatusCode.Unauthorized)); } var db = new MainContext(); var user = ServiceUser.GetLoggedUser(userName, db); if (user == null) { return(Response.AsJson("Logged user with bad userName", HttpStatusCode.InternalServerError)); } var body = this.Bind <PostPhotoBody>(); if (string.IsNullOrEmpty(body.Url)) { return(Response.AsJson("Body not completed.", HttpStatusCode.BadRequest)); } var placeId = parameters.id; var place = ServicePlace.GetPlaceById(placeId, db); if (place == null) { return(Response.AsJson("Place with this id not found.", HttpStatusCode.NotFound)); } ModelPhoto photo = ServicePlace.AddPhoto(place, user, body.Url, db); return(Response.AsJson(photo.GetView())); } catch (InDataError) { return(HttpStatusCode.InternalServerError); } catch (ModelBindingException) { return(Response.AsJson("Body not completed.", HttpStatusCode.BadRequest)); } catch (UnauthorizedAccessException) { return(Response.AsJson("You are not author of this place.", HttpStatusCode.Unauthorized)); } }