< Summary

Information
Class: Bookings.Api.Bookings.BookingsInMemRepository
Assembly: Bookings.Api
File(s): C:\Code\Bookings\Bookings.Api\Bookings\BookingsInMemRepository.cs
Line coverage
100%
Covered lines: 51
Uncovered lines: 0
Coverable lines: 51
Total lines: 71
Line coverage: 100%
Branch coverage
75%
Covered branches: 6
Total branches: 8
Branch coverage: 75%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor()100%11100%
Create(...)100%11100%
Create(...)100%11100%
GetAll()100%11100%
Get(...)100%11100%
Update(...)66.66%6687.5%
Update(...)100%1185.71%
Delete(...)100%11100%
GetAll()100%11100%
Get(...)100%11100%
Delete(...)100%22100%

File(s)

C:\Code\Bookings\Bookings.Api\Bookings\BookingsInMemRepository.cs

#LineLine coverage
 1using Bookings.Domain;
 2using System.Collections.Concurrent;
 3
 4namespace Bookings.Api.Bookings
 5{
 6    public class BookingsInMemRepository : IBookingsRepository
 7    {
 548        private readonly ConcurrentDictionary<Guid, Booking> _store = new();
 189        private readonly ConcurrentDictionary<BookingKey, Guid> _lookup = new();
 405610
 11        public KeyValuePair<Guid, Booking?> Create(Booking booking)
 608512        {
 202913            var bookingKey = BookingKey.From(booking);
 405614
 608515            var id = _lookup.AddOrUpdate(
 608516                bookingKey,
 608217                key => Guid.NewGuid(),
 203218                (key, value) => value);
 405619
 608520            var stored = _store.AddOrUpdate(
 202921                id,
 202622                key => booking,
 204023                (key, value) => value);
 824
 203725            return new(id, stored);
 202926        }
 27
 601628        public KeyValuePair<Guid, Booking?> Update(Guid id, Booking booking)
 702029        {
 702030            if (!_store.ContainsKey(id))
 601631                return new(id, null);
 32
 100433            if (_store.TryGetValue(id, out var existing) && Equals(existing, booking))
 201034                return new(id, existing);
 200835
 301036            var updated = _store.AddOrUpdate(
 100237                id,
 694338                key => booking,
 297839                (key, value) => booking);
 200840
 301041            var newKey = BookingKey.From(updated);
 100242            _lookup.AddOrUpdate(
 100243                newKey,
 301044                key => id,
 301045                (key, value) => id);
 200846
 301047            return new(id, updated);
 100448        }
 49
 50        public Dictionary<Guid, Booking> GetAll()
 551        {
 552            return _store.ToDictionary(); // Snapshot
 553        }
 54
 55        public KeyValuePair<Guid, Booking?> Get(Guid id)
 300856        {
 300857            _store.TryGetValue(id, out var found);
 300858            return new(id, found);
 300859        }
 60
 61        public KeyValuePair<Guid, Booking?> Delete(Guid id)
 100462        {
 100463            _store.TryRemove(id, out var deleted);
 64
 100465            if (deleted != null)
 100466                _lookup.TryRemove(BookingKey.From(deleted), out var removedId);
 67
 100468            return new(id, deleted);
 100469        }
 70    }
 71}