public async Task <Records> AddRecordAsync(RecordEntryModel newRecord)
        {
            var user = await userService.GetUserByUsernameAsync(newRecord.Username);

            var movie = await moviesService.GetMovieByIdAsync(newRecord.MovieId);

            if ((user == null) || (movie == null))
            {
                return(null);
            }

            var newRecordData = new Records()
            {
                User       = user,
                Movies     = movie,
                TakenDate  = DateTime.Now,
                ReturnDate = DateTime.Now.AddDays(movie.ReturnDays)
            };

            await db.Records.AddAsync(newRecordData);

            return(newRecordData);
        }
Exemple #2
0
        public async Task <ActionResult <RecordsDTO> > AddRecordEntry(string username, RecordEntryModel addRecord)
        {
            try
            {
                if (username != addRecord.Username)
                {
                    return(BadRequest());
                }

                Records record = await recordsService.AddRecordAsync(addRecord);

                if (record == null)
                {
                    return(BadRequest());
                }

                var location = link.GetPathByAction("GetUserRecordsById", "UserRecords", new { username = record.User.UserName, recordId = record.RecordsId });
                if (string.IsNullOrWhiteSpace(location))
                {
                    return(BadRequest());
                }
                //var recordDTO = mapper.Map<RecordsDTO>(record);

                if ((await recordsService.CommitAsync()) > 0)
                {
                    return(Created(location, record));
                }
            }
            catch (Exception)
            {
                return(this.StatusCode(StatusCodes.Status500InternalServerError, "Database Failure"));
            }
            return(BadRequest());
        }