public async Task <ResultModel> Delete(int id) { var result = new ResultModel(); try { RitualMoneyRecord recordToDelete = await _context.RitualMoneyRecords.FindAsync(id); if (recordToDelete == null) { result.IsSuccess = false; result.Message = "查無發財金紀錄."; return(result); } else { _context.RitualMoneyRecords.Remove(recordToDelete); await _context.SaveChangesAsync(); result.IsSuccess = true; result.Message = "刪除紀錄成功."; } } catch (Exception e) { result.IsSuccess = false; result.Message = "DB error."; } var accessToken = Request.Headers["Authorization"]; var user = await _TokenGetUserHelper.GetUser(accessToken); //logger.Info("userId=" + user.Id + ", username="******"\nDelete " + "RitualMoney id= " + id + " successfully."); return(result); }
public async Task <ResultModel> Put([FromBody] ReturnViewModel r) { ResultModel result = new ResultModel(); ///檢查輸入會員 是否存在於DB if (!string.IsNullOrEmpty(r.Name)) { r.Name = r.Name.Trim().ToUpper(); } var query = from m in _context.members where (string.IsNullOrEmpty(r.Name) ? true : m.Name.ToUpper().Equals(r.Name)) && (string.IsNullOrEmpty(r.Identity) ? true : m.Identity == r.Identity) select m; if (query.Count() == 0) { result.IsSuccess = false; result.Message = "查無會員"; return(result); } if (query.Count() > 1) { result.IsSuccess = false; result.Message = "查詢到多筆同名會員"; return(result); } RitualMoneyRecord recordToUpdate = new RitualMoneyRecord(); ///撈出該會員所有未還記錄 var records = _context.Entry(query.SingleOrDefault()) .Collection(q => q.RitualMoneyRecords) .Query() .Where(r => r.IsReturn == false) .ToList(); //無紀錄 if (records.Count() == 0) { result.IsSuccess = false; result.Message = "查無尚未還款的借用紀錄"; } else { try { recordToUpdate = records.FirstOrDefault(); recordToUpdate.IsReturn = true; recordToUpdate.ReturnAmount = r.ReturnAmount; recordToUpdate.ReturnDate = DateTime.Now.ToLocalTime(); await _context.SaveChangesAsync(); result.IsSuccess = true; result.Message = "還願成功"; } catch (Exception e) { result.IsSuccess = false; result.Message = "DB error."; } } var accessToken = Request.Headers["Authorization"]; var user = await _TokenGetUserHelper.GetUser(accessToken); //logger.Info("userId=" + user.Id + ", username="******"\nEdit Return " + "RitualMoney id= " + recordToUpdate.RitualMoneyRecordId + " successfully."); return(result); }
public async Task <ResultModel> Post([FromBody] BorrowViewModel b) { ResultModel result = new ResultModel(); RitualMoneyRecord record = new RitualMoneyRecord(); if (!string.IsNullOrEmpty(b.Name)) { b.Name = b.Name.Trim().ToUpper(); } var query = from m in _context.members where (string.IsNullOrEmpty(b.Name) ? true : m.Name.ToUpper() == (b.Name)) && (string.IsNullOrEmpty(b.Identity) ? true : m.Identity == b.Identity) select m; //查無會員 if (query.Count() == 0) { result.IsSuccess = false; result.Message = "查無資料."; return(result); } //查詢到多筆同名會員 if (query.Count() > 1) { result.IsSuccess = false; result.Message = "查詢到多筆同名會員"; return(result); } // var recordQuery = from o in _context.RitualMoneyRecords where (o.MemberId == query.SingleOrDefault().MemberId) && (o.IsReturn == false) select o; if (recordQuery.Count() > 0) { result.IsSuccess = false; result.Message = "尚有未還願的借用紀錄"; return(result); } try { var member = query.SingleOrDefault(); record.MemberId = member.MemberId; record.BorrowDate = DateTime.Now.ToLocalTime(); record.BorrowAmount = b.BorrowAmount; //_context.RitualMoneyRecords.Add(record); await _context.RitualMoneyRecords.AddAsync(record); await _context.SaveChangesAsync(); result.IsSuccess = true; result.Message = "借用發財金成功."; } catch (Exception e) { result.IsSuccess = false; result.Message = "DB error."; } var accessToken = Request.Headers["Authorization"]; var user = await _TokenGetUserHelper.GetUser(accessToken); //logger.Info("userId=" + user.Id + ", username="******"\nCreate " + "RitualMoney id= " + record.RitualMoneyRecordId +" successfully."); return(result); }