public async Task <IActionResult> Create(RequestSeasonalActivities seasonalActivity) { if (seasonalActivity.file != null) { var savePath = Path.Combine(Directory.GetCurrentDirectory(), @"wwwroot\Upload", seasonalActivity.file.FileName); using (var stream = new FileStream(savePath, FileMode.Create)) { await seasonalActivity.file.CopyToAsync(stream); } seasonalActivity.ImgUrl = "/Upload/" + seasonalActivity.file.FileName; } try { var target = new SeasonalActivities() { Url = seasonalActivity.Url, ImgUrl = seasonalActivity.ImgUrl, DownDate = seasonalActivity.DownDate, PublishDate = seasonalActivity.PublishDate, Title = seasonalActivity.Title }; _context.SeasonalActivities.Add(target); await _context.SaveChangesAsync(); return(Ok()); } catch (Exception ex) { return(StatusCode(Status400BadRequest, new ResponseMessage { Message = "查無資訊" })); } }
public async Task <IActionResult> Edit(RequestSeasonalActivities seasonalActivity) { var seasonal = _context.SeasonalActivities.Where(r => r.ID == seasonalActivity.Id); if (seasonal.Any()) { try { var data = seasonal.FirstOrDefault(); if (seasonalActivity.file != null) { var savePath = Path.Combine(Directory.GetCurrentDirectory(), @"wwwroot\Upload", seasonalActivity.file.FileName); using (var stream = new FileStream(savePath, FileMode.Create)) { await seasonalActivity.file.CopyToAsync(stream); } data.ImgUrl = "/Upload/" + seasonalActivity.file.FileName; } data.Title = seasonalActivity.Title; data.Url = seasonalActivity.Url; data.PublishDate = seasonalActivity.PublishDate; data.DownDate = seasonalActivity.DownDate; await _context.SaveChangesAsync(); return(Ok()); } catch (Exception ex) { return(StatusCode(Status400BadRequest, new ResponseMessage { Message = ex.Message })); } } else { return(StatusCode(Status400BadRequest, new ResponseMessage { Message = "查無資訊" })); } }