public ActionResult Create(DiaryLogViewModel diaryLogVM) { DateTime?nowDate = System.DateTime.Now; if (!IsSessionExist()) { return(RedirectToAction("Login", "Account")); } if (ModelState.IsValid) { diaryLogVM.UserId = Convert.ToInt32(Session["Id"]); diaryLogVM.CreateId = Session["Account"].ToString(); diaryLogVM.CreateDate = nowDate; diaryLogVM.UpdateId = Session["Account"].ToString(); diaryLogVM.UpdateDate = nowDate; Mapper.CreateMap <DiaryLogViewModel, DiaryLog>(); DiaryLog diaryLog = Mapper.Map <DiaryLog>(diaryLogVM); _genericRepository.Insert(diaryLog); return(RedirectToAction("Index")); } return(View(diaryLogVM)); }
public async Task <IActionResult> PutDiaryLog([FromRoute] int id, [FromBody] DiaryLog diaryLog) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != diaryLog.Id) { return(BadRequest()); } _context.Entry(diaryLog).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!DiaryLogExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public ActionResult DeleteConfirmed(int id) { DiaryLog diaryLog = _genericRepository.GetById(id); _genericRepository.Delete(diaryLog); return(RedirectToAction("Index")); }
public async Task <IActionResult> PostDiaryLog([FromBody] DiaryLog diaryLog) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } _context.DiaryLog.Add(diaryLog); await _context.SaveChangesAsync(); return(CreatedAtAction("GetDiaryLog", new { id = diaryLog.Id }, diaryLog)); }
public DiaryLogRateViewModel(Window window, DiaryLog diaryLog) : base(window) { mWindow = window; WindowMinimumWidth = 900; WindowMinimumHeight = 550; DiaryLog = diaryLog ?? new DiaryLog(); DiaryLog.DiaryLogRate ??= new DiaryLogRate(); Title = $"Rate Your Day: {diaryLog.Date}"; CloseCommand = new RelayCommand(p => CloseWindow()); LoadFeelings(); }
public void UpdateDiaryLog(DiaryLog diaryLog, DiaryLog newDiaryLog, DateTime diaryLogDate, string account, int userId) { using (DiaryLogRepository _repo = new DiaryLogRepository()) { diaryLog.DiaryLogItem = newDiaryLog.DiaryLogItem; diaryLog.DiaryLogContents = newDiaryLog.DiaryLogContents; diaryLog.DiaryLogStatus = newDiaryLog.DiaryLogStatus; diaryLog.DiaryLogHours = newDiaryLog.DiaryLogHours; diaryLog.DiaryLogSituation = newDiaryLog.DiaryLogSituation; diaryLog.DiaryLogSolve = newDiaryLog.DiaryLogSolve; diaryLog.UpdateDate = DateTime.Now; diaryLog.UpdateId = account; _repo.Edit(diaryLog); } }
/// <summary> /// Current constructor /// </summary> /// <param name="window">Parent window</param> /// <param name="diaryLog">Day.DiaryLog</param> /// <param name="date">Day.Date</param> public AddDiaryLogViewModel(Window window, DateTime date, DiaryLog diaryLog = null) : base(window) { mWindow = window; WindowMinimumHeight = 600; WindowMinimumWidth = 1100; DiaryLog = diaryLog != null ? diaryLog : new DiaryLog(); DiaryLog.Date = date != null ? date : DateTime.Now; Title = $"Day: {DiaryLog.Date.ToShortDateString()}"; CloseCommand = new RelayCommand(p => AddOrUpdate()); ShowHistoryLogCommand = new RelayParameterizedCommand(ShowHistoryLog); OpenHyperlinkCommand = new RelayParameterizedCommand(OpenHyperlink); OpenDayRateCommand = new RelayCommand(p => OpenDayRate()); LoadLog(); }
public void ModidDiaryLogy(List <DiaryLog> diaryLogs, DateTime diaryLogDate, string account, int userId) { using (DiaryLogRepository _repo = new DiaryLogRepository()) { foreach (var item in diaryLogs) { DiaryLog diaryLog = _repo.GetById(item.DiaryLogId); if (diaryLog != null) { UpdateDiaryLog(diaryLog, item, diaryLogDate, account, userId); } else { InsertDiaryLog(item, diaryLogDate, account, userId); } } } }
public ActionResult AjaxCreate(DiaryLogNewCreateVM model) { if (model.DiaryLogs == null || model.DiaryLogDate == null) { return(Json("false,")); } //移除不需要的驗證 foreach (var item in ModelState.Keys) { if (item.Contains("DiaryLogItems")) { // ModelState.Remove(item); ModelState[item].Errors.Clear(); } } if (!ModelState.IsValid) { return(Json("false,")); } int userId = Convert.ToInt32(Session["Id"].ToString()); string account = Session["Account"].ToString(); DiaryLogService _diaryLogService = new DiaryLogService(); foreach (var item in model.DiaryLogs) { DiaryLog diaryLog = new DiaryLog(); diaryLog.DiaryLogItem = item.DiaryLogItem; diaryLog.DiaryLogContents = item.DiaryLogContents; diaryLog.DiaryLogStatus = item.DiaryLogStatus; diaryLog.DiaryLogHours = item.DiaryLogHours; diaryLog.DiaryLogSituation = item.DiaryLogSituation; diaryLog.DiaryLogSolve = item.DiaryLogSolve; _diaryLogService.InsertDiaryLog(diaryLog, model.DiaryLogDate, account, userId); } return(Json("true," + userId.ToString())); }
public ActionResult Edit(DiaryLogViewModel diaryLogVM) { if (!IsSessionExist()) { return(RedirectToAction("Login", "Account")); } if (ModelState.IsValid) { diaryLogVM.UpdateId = Session["Account"].ToString(); diaryLogVM.UpdateDate = System.DateTime.Now; Mapper.CreateMap <DiaryLogViewModel, DiaryLog>(); DiaryLog diaryLog = Mapper.Map <DiaryLog>(diaryLogVM); _genericRepository.Edit(diaryLog); return(RedirectToAction("Index")); } return(View(diaryLogVM)); }
public void InsertDiaryLog(DiaryLog diaryLogs, DateTime diaryLogDate, string account, int userId) { using (DiaryLogRepository _repo = new DiaryLogRepository()) { DiaryLog newDiaryLog = new DiaryLog(); newDiaryLog.DiaryLogDate = diaryLogDate; newDiaryLog.UserId = userId; newDiaryLog.DiaryLogItem = diaryLogs.DiaryLogItem; newDiaryLog.DiaryLogContents = diaryLogs.DiaryLogContents; newDiaryLog.DiaryLogStatus = diaryLogs.DiaryLogStatus; newDiaryLog.DiaryLogHours = diaryLogs.DiaryLogHours; newDiaryLog.DiaryLogSituation = diaryLogs.DiaryLogSituation; newDiaryLog.DiaryLogSolve = diaryLogs.DiaryLogSolve; newDiaryLog.CreateDate = DateTime.Now; newDiaryLog.CreateId = account; newDiaryLog.UpdateDate = DateTime.Now; newDiaryLog.UpdateId = account; _repo.Insert(newDiaryLog); } }
// GET: DiaryLogs/Delete/5 public ActionResult Delete(int?id) { if (!IsSessionExist()) { return(RedirectToAction("Login", "Account")); } if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } DiaryLog diaryLog = _genericRepository.GetById(id.Value); if (diaryLog == null) { return(HttpNotFound()); } Mapper.CreateMap <DiaryLog, DiaryLogViewModel>(); DiaryLogViewModel diaryLogVM = Mapper.Map <DiaryLogViewModel>(diaryLog); return(View(diaryLogVM)); }
public async Task <IActionResult> UploadAndPost([FromForm] DiaryEntry diaryEntry) { if (!MultipartRequestHelper.IsMultipartContentType(Request.ContentType)) { return(BadRequest($"Expected a multipart request, but got {Request.ContentType}")); } else { try { // A diaryEntry could have multiple files or None. // We have to store the URI of each file in the DiaryImage table with its , associated with a eventID. // Save the event ID in a variable. // Create a diary log and upload the details. // Make the Story URI /* if (not zero files) * there is files * go through each file * upload the file to a blob and get the URI * Put the URI along with the width/height in a new diary image instance with the eventID created * Save changes */ // CloudBlockBlob uploadedTextBlob = await UploadTextToBlob(diaryEntry.Story); int eventID; // Successfully uploaded Blob and identifier is found DiaryLog diaryLogInstance = new DiaryLog { UserId = diaryEntry.UserID, EventName = diaryEntry.Event, StoryUrl = diaryEntry.Story, StartTime = diaryEntry.StartTime, EndTime = diaryEntry.EndTime }; _context.DiaryLog.Add(diaryLogInstance); await _context.SaveChangesAsync(); eventID = diaryLogInstance.Id; // if 0 images, nothing will be attempted to upload if (diaryEntry.Images != null && diaryEntry.Images.Any()) { foreach (IFormFile imageInstance in diaryEntry.Images) { // Read images as a stream of data so it can be uploaded to blob using (System.IO.Stream stream = imageInstance.OpenReadStream()) { // Create and upload a blob for the image. CloudBlockBlob uploadedImageBlob = await UploadImageToBlob(imageInstance.FileName, null, stream); string uploadedImageBlobURI = uploadedImageBlob.StorageUri.ToString(); if (!string.IsNullOrEmpty(uploadedImageBlobURI)) { System.Drawing.Image uploadedImage = System.Drawing.Image.FromStream(stream); // If there is a valid URI, then upload DiaryImage diaryImageInstance = new DiaryImage { // Problem : This is trying to upload before primary key eventId is made EntryId = eventID, ImageURL = uploadedImageBlob.SnapshotQualifiedUri.AbsoluteUri, Height = uploadedImage.Height.ToString(), Width = uploadedImage.Width.ToString() }; // Add this new instance to the dbset (collection of entities) in memory _context.DiaryImage.Add(diaryImageInstance); } } } } // Save all the changes to the database await _context.SaveChangesAsync(); return(Ok($"The file {diaryEntry.Event} has been succesfully uploaded")); #region /* * using (System.IO.Stream stream = diaryEntry.Image.OpenReadStream()) * { * // create and upload Blob for Image and Story * CloudBlockBlob uploadedImageBlob = await UploadImageToBlob(diaryEntry.Image.FileName, null, stream); * CloudBlockBlob uploadedTextBlob = await UploadTextToBlob(diaryEntry.Story); * * // The URI is an identifier for the blob. Its a more general version of a url. * string uploadedImageBlobURI = uploadedImageBlob.StorageUri.ToString(); * string uploadedTextBlobURI = uploadedTextBlob.StorageUri.ToString(); * * // If the URI is empty (there is no identifier for the blob), then something went wrong * if (string.IsNullOrEmpty(uploadedImageBlobURI) || string.IsNullOrEmpty(uploadedTextBlobURI)) * { * return BadRequest("Error when uploading: Identifier not found. Please Try Again"); * } * else * { * // Can now begin uploading data to database * * System.Drawing.Image image = System.Drawing.Image.FromStream(stream); * * // Create an instance of a Diary Log * DiaryLog diaryLog = new DiaryLog * { * EventName = diaryEntry.Event, * StoryUrl = uploadedTextBlob.SnapshotQualifiedUri.AbsoluteUri, // Get the URI for the text blob * StartTime = diaryEntry.StartTime, * EndTime = diaryEntry.EndTime, * ImageUrl = uploadedImageBlob.SnapshotQualifiedUri.AbsoluteUri, // Get the URI for the image blob * Height = image.Height.ToString(), * Width = image.Width.ToString() * }; * * // Adding the files to the database * _context.DiaryLog.Add(diaryLog); * await _context.SaveChangesAsync(); * * return Ok($"File: {diaryEntry.Event} has been successfully uploaded to the database"); * } * } */ #endregion } catch (Exception e) { return(BadRequest($"An error has occured g: {e.Message}")); } } }