Browse Source

Fix withdrawal logic

Samuel W. Flint 3 years ago
parent
commit
d989734165
1 changed files with 41 additions and 17 deletions
  1. 41 17
      library

+ 41 - 17
library

@@ -9,7 +9,7 @@ export LIBRARY_DEFAULT_FIELDS=${LIBRARY_DEFAULT_FIELDS:-ID,Title,Author,LCCN,Loc
 
 # help-message, /home/swflint/Projects/library/library.org
 if [[ $# -eq 0 ]] ; then
-    echo "library [ help | query | add | git | bulk-add | report | edit | edit-matching | loan | return-book | init ]"
+    echo "library [ help | query | add | git | bulk-add | report | edit | edit-matching | loan | return-book | withdraw | drop-withdrawn | init ]"
     exit
 fi
 
@@ -27,6 +27,8 @@ edit:       Edit the value of a specified field in a specified record.
 edit-matching: Edit records matching a give expression
 loan:       Loan a book out.
 return-book:Process a book return
+withdraw:   Withdraw a specified book
+drop-withdrawn: Drop withdrawn books
 init:       Initialize the database.
 EOF
 }
@@ -170,21 +172,13 @@ function do_edit {
     shift
     FIELD=$1
     shift
+    VALUE=$1
+    shift
+
+    TMPDIR=. recset -e "ID = ${ID}" \
+          -f "${FIELD}" -S "${VALUE}" \
+          "${LIBRARY_FILE}"
 
-    if [[ $FIELD = "Withdrawn" ]] ; then
-        TMPDIR=. recset -e "ID = ${ID}" \
-               -f "Withdrawn" -S "`date +"%a, %d %b %Y %H:%M:%S %z"`" \
-               "${LIBRARY_FILE}"
-        TMPDIR=. recset -e "ID = ${ID}" \
-              -f "Location" -S "WITHDRAWN" \
-              "${LIBRARY_FILE}"
-    else
-        VALUE=$1
-        shift
-        TMPDIR=. recset -e "ID = ${ID}" \
-               -f "${FIELD}" -S "${VALUE}" \
-               "${LIBRARY_FILE}"
-    fi
     do_git add `basename "${LIBRARY_FILE}"`
     do_git commit -m "Edited record id ${ID}"
 }
@@ -201,8 +195,8 @@ function do_edit_exp {
     VALUE=$1
     shift
     TMPDIR=. recset -e "${MATCHEXPRESSION}" \
-           -f "${FIELDNAME}" -S "${VALUE}" \
-           "${LIBRARY_FILE}"
+          -f "${FIELDNAME}" -S "${VALUE}" \
+          "${LIBRARY_FILE}"
     do_git add $(basename "${LIBRARY_FILE}")
     if [[ $1 != "" ]] ; then
         do_git commit -m "${1}"
@@ -212,6 +206,28 @@ function do_edit_exp {
 }
 # edit-field ends here
 
+# withdraw-books, /home/swflint/Projects/library/library.org
+function do_withdraw {
+    if [ $# -lt 1 ] ; then
+        echo "library withdraw id"
+        exit 1
+    fi
+    ID=$1
+    TMPDIR=. recset -e "ID = ${ID}" \
+          -f "Withdrawn" -S "`date +"%a, %d %b %Y %H:%M:%S %z"`" \
+          -f "Location" -S "WITHDRAWN" \
+          "${LIBRARY_FILE}"
+    do_git add $(basename ${LIBRARY_FILE})
+    do_git commit -m "Withdrew book ${ID}"
+}
+
+function remove_withdrawn {
+    TMPDIR=. recdel -t Book \
+          -e "Location ~ \"WITHDRAWN\"" \
+          "${LIBRARY_FILE}"
+}
+# withdraw-books ends here
+
 # initialize-database, /home/swflint/Projects/library/library.org
 function initialize {
     OLD=`pwd`
@@ -337,6 +353,14 @@ case "${COMMAND}" in
         do_return "$@"
         exit
         ;;
+    withdraw)
+        do_withdraw "$@"
+        exit
+        ;;
+    drop-withdrawn)
+        remove_withdrawn "$@"
+        exit
+        ;;
     init)
         initialize
         exit