Esempio n. 1
0
        public ActionResult UpdateRecordInWallet([FromRoute] int groupId, [FromRoute] int wallet_record_id, [FromBody] AddMoneyRecord moneyRecord)
        {
            Console.WriteLine("change a money record");
            decimal totalMoney = 0;
            List <ShowWalletRecord> records = new List <ShowWalletRecord>();

            using (SqlConnection sqlConnection = new SqlConnection(Global.connect_string))
            {
                sqlConnection.Open();
                string strCreate = @"Update WalletInfo SET MoneyTrack=@moneyTract , WalletDescribe=@WalletDescribe,RecordDate=@recordDate WHERE WalletRecordId = @recordId
                                       SELECT WalletRecordId, MoneyTrack, WalletDescribe, RecordDate FROM[WalletInfo] WHERE GroupId = @groupId ORDER BY RecordDate ASC";
                using (SqlCommand cmd = new SqlCommand(strCreate, sqlConnection))
                {
                    cmd.Parameters.AddWithValue("@moneyTract", moneyRecord.MoneyTrack);
                    cmd.Parameters.AddWithValue("@WalletDescribe", moneyRecord.WalletDescribe);
                    cmd.Parameters.AddWithValue("@recordDate", moneyRecord.RecordDate);
                    cmd.Parameters.AddWithValue("@recordId", wallet_record_id);
                    cmd.Parameters.AddWithValue("@groupId", groupId);
                    using (SqlDataReader reader = cmd.ExecuteReader())
                    {
                        if (reader.HasRows)
                        {
                            while (reader.Read())
                            {
                                ShowWalletRecord newObj = new ShowWalletRecord();
                                newObj.WalletRecordId = reader.GetInt32(0);
                                newObj.MoneyTrack     = reader.GetDecimal(1);
                                newObj.WalletDescribe = reader.GetString(2);
                                newObj.RecordDate     = reader.GetDateTime(3).ToString("yyyy-MM-dd");
                                totalMoney           += newObj.MoneyTrack;
                                newObj.TotalMoney     = totalMoney;
                                records.Add(newObj);
                            }
                            return(CreatedAtAction("UpdateRecordInWallet", new { errorcode = -1, msg = "success get wallet record", WalletRecords = records }));
                        }
                        else
                        {
                            return(CreatedAtAction("UpdateRecordInWallet", new { errorcode = 400, msg = "the wallet yet found" }));
                        }
                    }
                }
            }
        }
Esempio n. 2
0
        public ActionResult AddRecordInWallet([FromBody] AddMoneyRecord moneyRecord, [FromRoute] int groupId)
        {
            Console.WriteLine("add new money record");
            decimal totalMoney = 0;
            List <ShowWalletRecord> records = new List <ShowWalletRecord>();

            using (SqlConnection sqlConnection = new SqlConnection(Global.connect_string))
            {
                sqlConnection.Open();
                string strCreate = @"INSERT INTO WalletInfo(MoneyTrack,WalletDescribe,RecordDate,GroupId) 
                                        VALUES(@moneyTract, @WalletDescribe, convert(datetime,@recordDate, 20), @groupId)
                                        SELECT WalletRecordId, MoneyTrack, WalletDescribe, RecordDate FROM[WalletInfo] WHERE GroupId = @groupId ORDER BY RecordDate ASC
                                        ";
                using (SqlCommand cmd = new SqlCommand(strCreate, sqlConnection))
                {
                    cmd.Parameters.AddWithValue("@moneyTract", moneyRecord.MoneyTrack);
                    cmd.Parameters.AddWithValue("@WalletDescribe", moneyRecord.WalletDescribe);
                    cmd.Parameters.AddWithValue("@recordDate", moneyRecord.RecordDate);
                    cmd.Parameters.AddWithValue("@groupId", groupId);
                    using (SqlDataReader reader = cmd.ExecuteReader())
                    {
                        while (reader.HasRows && reader.Read())
                        {
                            ShowWalletRecord newObj = new ShowWalletRecord();
                            newObj.WalletRecordId = reader.GetInt32(0);
                            newObj.MoneyTrack     = reader.GetDecimal(1);
                            newObj.WalletDescribe = reader.GetString(2);
                            newObj.RecordDate     = reader.GetDateTime(3).ToString("yyyy-MM-dd");
                            totalMoney           += newObj.MoneyTrack;
                            newObj.TotalMoney     = totalMoney;
                            records.Add(newObj);
                        }
                    }
                    //return ShowGroupWallet(groupId);
                    return(CreatedAtAction(nameof(AddRecordInWallet), new { errorcode = -1, msg = "success add record", WalletRecords = records }));
                }
            }
        }