Пример #1
0
        public async Task <IActionResult> borrow(String code)
        {
            try {
                code = Encoding.UTF8.GetString(Convert.FromBase64String(code));
                int bookcase_id = Convert.ToInt32(code.Substring(6));

                Borrow borrowed = await _borrowRepository.FindByBookcase(bookcase_id);

                if (borrowed != null && borrowed.return_date == null)
                {
                    return(BadRequest(new Respone(400, "Borrowed", null)));
                }

                Bookcase bookcase = await _bookcaseRepository.Get(bookcase_id);

                if (bookcase == null)
                {
                    return(NotFound(new Respone(404, "Not Found", null)));
                }

                var  username = User.FindFirst(ClaimTypes.NameIdentifier).Value;
                User user     = await _userRepository.FindByUsername(username);

                if (user == null)
                {
                    return(NotFound(new Respone(404, "Not Found", null)));
                }

                if (bookcase.user_id == user.user_id)
                {
                    return(BadRequest(new Respone(400, "Failed", null)));
                }

                Borrow borrow = new Borrow {
                    user_id_borrow = user.user_id,
                    bookcase_id    = bookcase_id
                };

                await _borrowRepository.Add(borrow);

                return(Ok(new Respone(200, "ok", null)));
            } catch (Exception e) {
                return(BadRequest(new Respone(400, "Failed", null)));
            }
        }